Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to correctly use a debounce?

Asked by
Scerzy 85
8 years ago

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

Log in to vote
0
Answered by 8 years ago

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)
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 Scerzy 85 — 8y
Ad

Answer this question