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)
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.
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)
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.