I have this gate that slides to open and close. There's a button with a ClickDetector to open and close the gate. Now I got the buttons to work, but there's a slight problem. If you spam click either button, the gate will slide further than intended to and will ultimately leave its frame. Here's the script within the open button.
script.Parent.ClickDetector.MouseClick:connect(function() local Gate=game.Workspace.gate.GateDoor.Gate local Clicked = false if Clicked==false then Clicked =true print("debounce true") for i=1,Gate.Size.y*5+1 do Gate.CFrame=Gate.CFrame*CFrame.new(0,-0.2,0) wait() end wait() Clicked=false print("debounce false") end end)
clicking a lot will have it print "debounce true" many times and then "debounce false" for just as many times. How do I make it so that you cannot click this button more than once in a row and you can't click it while the loop is running? Thank you.
Local values only exist within the scope which they are declared. Simply move your code about to fix your problem.
local Clicked = false; script.Parent.ClickDetector.MouseClick:connect(function() local Gate=game.Workspace.gate.GateDoor.Gate if Clicked==false then Clicked =true print("debounce true") for i=1,Gate.Size.y*5+1 do Gate.CFrame=Gate.CFrame*CFrame.new(0,-0.2,0) wait() end wait() Clicked=false print("debounce false") end end)