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:
01 | local function onTouched(otherPart) |
02 | local character = otherPart.Parent |
03 | if character then |
04 | local player = game.Players:GetPlayerFromCharacter(character) |
05 | if player then |
06 | game.Workspace.Part.Door.CanCollide = false |
07 | game.Workspace.Part.Door.Transparency = 1 |
08 | wait( 5 ) |
09 | game.Workspace.Part.Door.CanCollide = true |
10 | game.Workspace.Part.Door.Transparency = 0 |
11 | return onTouched |
12 | end |
13 | end |
14 | end |
15 |
16 | 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.
1 | 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.
1 | debounce = false |
2 |
3 | game.Workspace.Part.Touched:Connect( function (plr) |
4 |
5 | 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.
01 | debounce = false |
02 |
03 | game.Workspace.Part.Touched:Connect( function (plr) |
04 | debouce = true |
05 | game.Workspace.Door.Transparency = 1 |
06 | game.Workspace.Door.CanCollide = false |
07 | wait( 5 ) |
08 | game.Workspace.Door.Transparency = 0 |
09 | game.Workspace.Door.CanCollide = false |
10 | debouce = false |
11 | 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.
01 | local debounce = true --//Declaring the debounce variable |
02 |
03 | local function onTouched(otherPart) |
04 | 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 |
05 |
06 | local character = otherPart.Parent |
07 | if character then |
08 | local player = game.Players:GetPlayerFromCharacter(character) |
09 | if player then |
10 | debounce = false --//Setting it to false so the touch function doesn't fire more than once during this 5 second interval |
11 |
12 | game.Workspace.Part.Door.CanCollide = false |
13 | game.Workspace.Part.Door.Transparency = 1 |
14 | wait( 5 ) |
15 | game.Workspace.Part.Door.CanCollide = true |
Otherwise everything else seems fine, hope it works.
script.Parent.Touched:Connect(function(onTouched) ?