Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to correctly use a debounce?

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.

1 answer

✓ Accepted answer

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)

1 comment

0

Thanks, but now I have to figure out how to prevent someone from clicking both buttons at the same time and having someone click the same button twice

This discussion is preserved as it was. New answers, comments, and votes are closed.