What I want to do is this: Put a debounce in the script. SO then if you click the button the gate will slide thorough. But if you click it again it will slide back. But you have to wait a couple seconds for it to slide back. I am also toying around with it to see if I can make it not make the gate go too far if it is repetedly pressed. I would love it if someone would help me!
DoorSystem = script.Parent Door = DoorSystem.Door1 DoorPad = DoorSystem.Open.ClickDetector local padPressed = false function Slide() if not padPressed then padPressed = true for i = 1, 500 do Door.CFrame = Door.CFrame + Vector3.new(0, 0, 0.1) wait() end end end DoorPad.MouseClick:connect(Slide)
This isn't quite a standard debounce.
Basically, we have to make new button presses interrupt old ones. Instead of doing the loop in the event, though, it really is easier to make the event just write the direction, rather than actually repeatedly act on it:
DoorSystem = script.Parent Door = DoorSystem.Door1 DoorPad = DoorSystem.Open.ClickDetector home = Door.CFrame -- Where the door STARTS open = 0 -- How far (in studs) the door is open direction = 0 speed = 0.3 -- studs/second function Slide() -- Flip direction (turn around) if direction <= 0 then direction = 1 else direction = -1 end end DoorPad.MouseClick:connect(Slide) while true do open = open + direction * speed * wait() open = math.max(0, open) -- Not a negative opening open = math.min(50, open) -- At most 50 studs open Door.CFrame = home + Vector3.new(0, 0, open) end
This won't close on its own, though.
A partial solution is this:
function Slide() -- Flip direction (turn around) if direction <= 0 then direction = 1 wait(5) direction = -1 else direction = -1 end end
The problem is input like this:
wait(5)
from first click just fired, so door closesThat interruption in the last moment is the problem. Basically, we need to measure if there has been another click in the last 5 seconds:
lastClick = 0 function Slide() -- Flip direction (turn around) lastClick = tick() now = tick() if direction <= 0 then direction = 1 wait(5) if lastClick <= now then direction = -1 end else direction = -1 end end