|
I am creating a door script, where whenever you press 'E', the door will open. However, I am having a problem where it will only register you pressing the 'E' key whenever you have it down right as you touch the invisible block to open the door.
Here is my code:
local deb = false
function touch(hit)
if deb == false then
deb = true
local UIP = game:GetService ("UserInputService")
if UIP:IsKeyDown(101) then
if game.Workspace.Door1.CanCollide == true then
game.Workspace.Door1.CanCollide = false
elseif game.Workspace.Door1.CanCollide == false then
game.Workspace.Door1.CanCollide = true
end
end
wait(1)
deb = false
end
end
script.Parent.Touched:connect(touch)
How do I make it so it will register the E key always while you're inside the block? |
|
|
iiNemoJoin Date: 2013-07-22 Post Count: 2380 |
game:GetService('UserInputService'):Connect(function(Input)
if Input.KeyCode == Enum.KeyCode.E then
end
end) |
|
|
Nope, I changed the capitalization of Connect and it still didn't work. Most likely because a service can't be in the connection of a function, that needs to be touch. |
|
LeafDoodeJoin Date: 2017-05-29 Post Count: 3094 |
try
UIP:IsKeyDown(Enum.KeyCode.E) |
|
LeafDoodeJoin Date: 2017-05-29 Post Count: 3094 |
@iiNemo
"game:GetService("UserInputService"):connect(function(Input)" LOL?! |
|
|
@Leaf
It works, but only when you first come in contact with the block. If I stand inside the block and press E, it doesn't work. |
|
|
and yeah, the function idea was terrible lol |
|
LeafDoodeJoin Date: 2017-05-29 Post Count: 3094 |
then you need a variable |
|
|
As in a variable for the UIP? Or a variable to figure out if the player is still touching the block? |
|
LeafDoodeJoin Date: 2017-05-29 Post Count: 3094 |
UIS = game:GetService("UserInputService")
if UIS:IsKeyDown(Enum.KeyCode.E) then
pressed = true
end
block.Touched:connect(function(hit)
touch = true
end)
if pressed = true and touch = true then
--code
end |
|
LeafDoodeJoin Date: 2017-05-29 Post Count: 3094 |
if pressed == true and touched == true then*
--code
end |
|
|
Yeah, I've been messing with it for the past minutes, but it seems like the keycode thing isn't firing. The touch is just fine. |
|
|
yin_yangJoin Date: 2016-02-24 Post Count: 537 |
localscript local self = game.Players.LocalPlayer local pressed local ####### ##### ### = game:GetService("UserInputService") local door = workspace.door1 local db = false function thisdoorthing() if pressed and touched then -- code() end end uis.InputBegan:connect(function(thisinputthing) if thisinputthing.KeyCode == Enum.KeyCode.E then thisdoorthing() -- since the input is what should trigger the door, the function lies here end end) door.Touched:connect(function(hit) if not db then (db == true) local humanoid = hit.Parent:WaitForChild("Humanoid") if humanoid then touched = true end end db = false end) a mistake i saw in your code is that you have not applied a "verifier" to check whether or not an actual player is touching the door. without this humanoid checker the door can be opened at any time as long as there's something touching it. |
|
yin_yangJoin Date: 2016-02-24 Post Count: 537 |
(pathetic filter)
localscript
local self = game.Players.LocalPlayer
local pressed
local tch
local inputserv = game:GetService("UserInputService")
local door = workspace.door1
local db = false
function thisdoorthing()
if pressed and tch then
-- code()
end
end
inputserv.InputBegan:connect(function(thisinputthing)
if thisinputthing.KeyCode == Enum.KeyCode.E then
thisdoorthing() -- since the input is what should trigger the door, the function lies here
end
end)
door.Touched:connect(function(hit)
if not db then (db == true)
local humanoid = hit.Parent:WaitForChild("Humanoid")
if humanoid then
tch = true
end
end
db = false
end)
a mistake i saw in your code is that you have not applied a "verifier" to check whether or not an actual player is touching the door. without this humanoid checker the door can be opened at any time as long as there's something touching it. |
|
|
Do I need to have some setting on for this to work? I put a little print thing in to verify if it worked(I fixed one error I noticed) and it just isn't working. I ran it in a server too, and it still didn't work. I'm starting to wonder if I need to turn on a setting for this to work, and yes, this was in a localscript. |
|
|
|
Ugh, I guess I'll have to do something different for the door then. |
|
BaiYuniJoin Date: 2009-10-09 Post Count: 2861 |
Here's an idea.
Have 2 scripts:
1 that does the UserInputService (the local script),
and another Script that does the Touched and TouchEnded event (a regular ServerScript)
When the object is touched, the localscript will have the 'Disabled' property as false and when the TouchEnded event fires, the 'Disabled' property will be set to true. |
|