Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Whats wrong with my open and close door script?

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)

3 answers

✓ Accepted answer

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

1 comment

0

:O Thanks! I almost going to 200 points :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.

2 comments

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! (:

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.

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

3 comments

0

Is it wrong? Or are you giving me the answer. (:

0

It's wrong. Not the best with scripting, sorry.

0

If you not posting a explaination, don't say it in answer! (:

This discussion is preserved as it was. New answers, comments, and votes are closed.