Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Whats wrong with my open and close door script?

Asked by 5 years ago

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:

01local 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
14end
15 
16script.Parent.Touched:Connect(onTouched)

3 answers

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
5 years ago

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.

1debounce = 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.

1debounce = false
2 
3game.Workspace.Part.Touched:Connect(function(plr)
4 
5end)

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.

01debounce = false
02 
03game.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
11end)

Tada! A working script. Simple as you think! winks

Bye! Have a good day in Roblox Studio, I hope you like ROBLOX! :D

0
:O Thanks! I almost going to 200 points :D Xapelize 2658 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

01local debounce = true --//Declaring the debounce variable
02 
03local 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
View all 24 lines...

Otherwise everything else seems fine, hope it works.

0
Im very certain your script works, but after in the output when I tested if it worked, it says "Door is not a valid member of Part" in the output. And the 2 lines below wait(5) wont work, idk why, do you know whats wrong with this? Anyways ty for your help! (: BaconPro1 2 — 5y
0
If "Door" is just in workspace, it should be "game.Workspace.Door" otherwise make sure that the "Door" is inside "Part" in workspace. Without more context it'll be hard to help you more. CeramicTile 847 — 5y
Log in to vote
-2
Answered by
KingDomas 153
5 years ago

script.Parent.Touched:Connect(function(onTouched) ?

0
Is it wrong? Or are you giving me the answer. (: BaconPro1 2 — 5y
0
It's wrong. Not the best with scripting, sorry. KingDomas 153 — 5y
0
If you not posting a explaination, don't say it in answer! (: Xapelize 2658 — 5y

Answer this question