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

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

thank you so much for your help

its not letting me change the transparency of the door do you know why?

Where you set the CanCollide, also do Door.Transparency = 1 (to make it transparent)

nvm I found it I just had the wrong properties

sry how would I make it so there are multiple doors and multiple buttons so 1 button won’t open all the doors

You can put a script in each door to do that.
Have a look at how I scripted the swinging door in my post above, you can see how I used the input from the Part that’s being stepped on to activate the door.

If you want i can do a model with 2 doors and 2 button for you to understand how it works to open one door with one button and the other one with another button. It’s pretty simple you’ll see.

ok that would be great I was trying to rename the door to door1 and change all the “door” in the script to “door1” but it didn’t work