I want to make where a player steps on a part and the "door" opens so they can go through it but closes after 5 seconds. The "Door" has no script, its just a part I named for this script fyi. Here's what I got so far:
local function onTouched(otherPart) local character = otherPart.Parent if character then local player = game.Players:GetPlayerFromCharacter(character) if player then game.Workspace.Part.Door.CanCollide = false game.Workspace.Part.Door.Transparency = 1 wait(5) game.Workspace.Part.Door.CanCollide = true game.Workspace.Part.Door.Transparency = 0 return onTouched end end end script.Parent.Touched:Connect(onTouched)
Maybe you can try another script, simpler and short, and easier to learn.
I will declare the debounce value, that's @CeramicTile said.
Before this, you must make the "Part" and "Door"s' parent to Workspace, or the script won't work if those are not parented by Workspace.
Anyways, let insert debounce inside.
debounce = false
By the way, I think your script is way too messy. Let make shorter, don't need to use a function, let just fire the code while 'Part' got stepped.
debounce = false game.Workspace.Part.Touched:Connect(function(plr) end)
P.S. 1, When your code contained :Connect(function()
, remember to place the 'end' become 'end)'.
P.S. 2, I'll say again, the last of the line doesn't need to contain script.Parent.Touched:Connect(onTouched)
. Don't add yourself!
Ok, anyways, let continue our script, when Part got stepped, the door will change the properties.
debounce = false game.Workspace.Part.Touched:Connect(function(plr) debouce = true game.Workspace.Door.Transparency = 1 game.Workspace.Door.CanCollide = false wait(5) game.Workspace.Door.Transparency = 0 game.Workspace.Door.CanCollide = false debouce = false end)
Tada! A working script. Simple as you think! winks
Bye! Have a good day in Roblox Studio, I hope you like ROBLOX! :D
I would propose you add in a debounce variable so the touch event doesn't fire multiple times during the 5 second interval that the door it already open.
local debounce = true --//Declaring the debounce variable local function onTouched(otherPart) if (not debounce) then return end --//We are returning nil which will make sure that all the code below will not be executed because debounce is false local character = otherPart.Parent if character then local player = game.Players:GetPlayerFromCharacter(character) if player then debounce = false --//Setting it to false so the touch function doesn't fire more than once during this 5 second interval game.Workspace.Part.Door.CanCollide = false game.Workspace.Part.Door.Transparency = 1 wait(5) game.Workspace.Part.Door.CanCollide = true game.Workspace.Part.Door.Transparency = 0 debounce = true --//This will allow the code above to get run again --return onTouched --//We don't need to return anything at this point end end end script.Parent.Touched:Connect(onTouched)
Otherwise everything else seems fine, hope it works.
script.Parent.Touched:Connect(function(onTouched) ?