How to make a door open when you step on a button

i want to make a button that turns cancollide off when you touch a button

Put this script inside your button.

local door = game.Workspace.Door -- make a part in workspace and name it door

script.Parent.Touched:Connect(function() -- the touch event
	door.CanCollide = false
	task.wait(2) -- wait for 2 seconds
	door.CanCollide = true
end)
2 Likes

To explain what Chris’ script does, so you can make it on your own another time. Also Chris’ Script has some flaws, that I secured wasn’t in the one I posted below.

local Door = workspace:WaitForChild("Door") -- This is the part that you define as a door. If you put the script inside the DoorPart, you can just use "script.Parent" instead of "workspace:WaitForChild("Door")
local DoorDebounce = false
Door.Touched:Connect(function(Hit) -- Here, our "Door" is Touched. When that happens we Connect a Function. From that Event we get a value. We've named this value Hit in this case, you can call it whatever you want.
	if Hit.Parent:FindFirstChildWhichIsA("Humanoid") and not DoorDebounce then -- Now, let's say the Foot hit our Door. The Foot will be "Hit" in this case then. Then we take the Parent of Foot/Hit. (Which is in this case a character). We then check if the character has a "Humanoid" in it. This way, we can check that it's a character that has touched the part, and not random stuff like another random Part (example a ball that is rolling). All Characters (from players) has a Humanoid in them. We also check that DoorDebounce is false, this will prevent the script to run twice or even 20 times very rapidly. If we found a Humanoid and DoorDebounce is false.
		DoorDebounce = true -- Now we set DoorDebounce to true, so the door can't be activated again before the whole script here has ran.
		Door.CanCollide = false -- Now we set the cancollide to be false, so we can walk through it.
		task.wait(2) -- Task.wait is telling the script to wait (in this case 2 seconds) before proceeding.
		Door.CanCollide = true -- Now we set the cancollide to be true, so we can't walk through it.
		DoorDebounce = false -- Now we set DoorDebounce to false again, the door can now again be activated!
	end
end)
3 Likes

Or you could make a simple hinged door like this free model I made:
https://www.roblox.com/library/4865019005/Door-using-HingeConstraints-and-script

1 Like

how would i make it so the door doesnt close again i tried deleting the task wait and door cancollide true but it didnt work

it seems to make it so that when you touch the door it opens the door not when you touch the button it opens the door

Just write this code and it should work:

local Door = workspace:WaitForChild("Door")
local Button = workspace:WaitForChild("Button")
local ButtonDebounce = false
local DoorDebounce = false
Button.Touched:Connect(function(Hit) -- Here, our "Button" is Touched. When that happens we Connect a Function. From that Event we get a value. We've named this value Hit in this case, you can call it whatever you want.
	if Hit.Parent:FindFirstChildWhichIsA("Humanoid") and not DoorDebounce and not ButtonDebounce then -- Now, let's say the Foot hit our Button. The Foot will be "Hit" in this case then. Then we take the Parent of Foot/Hit. (Which is in this case a character). We then check if the character has a "Humanoid" in it. This way, we can check that it's a character that has touched the part, and not random stuff like another random Part (example a ball that is rolling). All Characters (from players) has a Humanoid in them. We also check that DoorDebounce and ButtonDebounce are false, this will prevent the script to run twice or even 20 times very rapidly. If we found a Humanoid and DoorDebounce (and ButtonDebounce) is false.
		DoorDebounce = true -- Now we set DoorDebounce to true, so the door can't be activated again before the whole script here has ran. 
        ButtonDebounce = true -- Now we set ButtonDebounce to true, so the button can't be activated again before the whole script here has ran.
		Door.CanCollide = false -- Now we set the cancollide to be false, so we can walk through it.
		task.wait(2) -- Task.wait is telling the script to wait (in this case 2 seconds) before proceeding.
		Door.CanCollide = true -- Now we set the cancollide to be true, so we can't walk through it.
		DoorDebounce = false -- Now we set DoorDebounce to false again, the door can now again be activated!
        ButtonDebounce = false -- Now we set ButtonDebounce to false again, the Button can now activate the door again!
	end
end)

And if you want it so the door is open forever then write this one and put it into your button, and it should work:

local door = game.Workspace.Door -- make a part in workspace and name it door

script.Parent.Touched:Connect(function() -- the touch event, also script.Parent means that the script will search for the thing that's above it so change it if you have something above your script who's not your door. For example just write script.Parent.Parent if your script is under two other parts and again and again. 
door.CanCollide = false
end)

I can’t check if it’s works because I’m not at home right now, so sorry if the script doesn’t work. I’ll try looking at it when I’m back home, i hope i helped you!

how can i make it so the door stays open forever?

Just put this script inside your button and it should work:

local door = game.Workspace:WaitForChild("Door") -- make a part in workspace and name it "Door" so the script can find your door.

script.Parent.Touched:Connect(function() -- the touch event, also script.Parent means that the script will search for the thing that's above it, so change it if you have something above your script who's not your door. For example just write script.Parent.Parent if your script is under two other parts and again and again. 
    door.CanCollide = false -- here we disable the CanCollide of the door so the player can go through it. 
end)

You can try this one as it have a debounce so it’s less laggy:

local Door = workspace:WaitForChild("Door")
local ButtonDebounce = false
local DoorDebounce = false
Script.Parent.Touched:Connect(function(Hit) -- Here, our "Button" is Touched. When that happens we Connect a Function. From that Event we get a value. We've named this value Hit in this case, you can call it whatever you want.
	if Hit.Parent:FindFirstChildWhichIsA("Humanoid") and not DoorDebounce and not ButtonDebounce then -- Now, let's say the Foot hit our Button. The Foot will be "Hit" in this case then. Then we take the Parent of Foot/Hit. (Which is in this case a character). We then check if the character has a "Humanoid" in it. This way, we can check that it's a character that has touched the part, and not random stuff like another random Part (example a ball that is rolling). All Characters (from players) has a Humanoid in them. We also check that DoorDebounce and ButtonDebounce are false, this will prevent the script to run twice or even 20 times very rapidly. If we found a Humanoid and DoorDebounce (and ButtonDebounce) is false.
		DoorDebounce = true -- Now we set DoorDebounce to true, so the door can't be activated again before the whole script here has ran. 
        ButtonDebounce = true -- Now we set ButtonDebounce to true, so the button can't be activated again before the whole script here has ran.
		Door.CanCollide = false -- Now we set the cancollide to be false, so we can walk through it.
        wait(1)
        DoorDebounce = false
        ButtonDebounce = false
	end
end)

Add a Debounce there or *it will get called multiple times

Yes that’s what I was doing it’s really hard on mobile to write a proper piece of code lol

Sorry didnt see that xD :smile:

Lol no worries, you just tried to help

btw could you help me a bit i am also in a bit of a pickle : p
I Need Help With Mouse - Help and Feedback / Scripting Support - DevForum | Roblox
(sorry if im being rude by posting this here)

Yes maybe i can help let me see

it doesnt seem to work the other script made by chris worker but the door closed after

the code makes it so when you touch the Door it makes the Door go cancollide off not when you touch the Button it makes the Door go cancollide off

You put the script in the button?

If yes, I’ll check the code in 30 mins to see what’s not working.

1 Like

Alright, so i fixed the script and made it a model so you don’t have to do anything with the script, just read the “README!” script so you can setup the model properly and make it works.

Have a great day!

I do not recommend using bool values for debounces, as it requires more lines of codes and it is not safe to use.

Let’s say you make a scripting error so the bool value never changes back to true, how do you fix that? Well you could handle the error with if statements, but if you want something reliable and fast that will never stop working even when there is an error, comparing time is your way.

local Players = game:GetService("Players")
local Button = workspace:WaitForChild("Button")
local Door = workspace:WaitForChild("Door")
local deb = 0 -- Define the time. It is set to 0 so you do not have to wait 2 seconds the first time before you can use the button without having it pressed at all
local DebTime = 2 --Debounce time
Button.Touched:Connect(function(Part)
	local player = Players:GetPlayerFromCharacter(Part.Parent)
	if player then --We do this check to check if the character touched the button
		if os.time() - deb >= DebTime then -- We take the current time and minus it with the old time and check if it is over 2 seconds
			deb = os.time() --Update time
			Door.CanCollide = not Door.CanCollide --Setting the CanCollide to the opposite
		end
	end
end)
1 Like