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

How would I add debounce to a buttongui? It keeps breaking the script.

Asked by 4 years ago
script.Parent.MouseButton1Click:Connect(function(plr)
    local Cooldown = 10
    local db = false
    if db ==  false then
        db = true
    for i = Cooldown, 0, -1 do
        wait(1)
        script.Parent.Text = "Cooldown: "..i
        if script.Parent.Text == "Cooldown: 0" then
            script.Parent.Text = "Spawn"
        end
        wait(Cooldown)
        db = false
        end
    end
end)

I'm not really sure on what to do.

0
local db = false should be outside the event aipiox123 1 — 4y

2 answers

Log in to vote
1
Answered by
Thetacah 712 Moderation Voter
4 years ago

I think there's some slight issues with your logic.

Firstly, your **db **and **Cooldown ** variables should not be inside of your function. The way you have it right now, whenever you click the TextButton, no matter what, db will always be false.

Next, I've deleted your if statement that detects when the text will be equal to "Cooldown:0". We know that by the time the forloop finishes, that it would be equal to that, so what you did was redundant and somewhat pointless(although it would work). Instead, I simply changed the text to spawn once the loop was done running (after 10 seconds).

Not quite sure what you were trying to do with the wait(Cooldown), but your loop ran ten times, for 1 second each, which equals 10 seconds. As such, I removed that part.

The working code should look like this:

local Cooldown = 10
local db = false
script.Parent.MouseButton1Click:Connect(function(plr)

    if db == false then
        db = true
        for i = Cooldown, 0, -1 do
            wait(1)
            script.Parent.Text = "Cooldown: "..i
        end
    script.Parent.Text = "Spawn"
    db = false
    end
end)
0
Worked! iiBuilder_Boy 27 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I'm not 100% sure what the problem is but it may have to do with the fact that you're putting the "wait() debounce = false" in the for loop?

Again, I'm not 100% sure so this may not work. But I had a similar problem once and I did something like this to change it:

local cooldown = 10
local debounce = false

script.Parent.MouseButton1Click:Connect(function()
    if not debounce then
        debounce = true

        for i = Cooldown, 0, -1 do
            wait(1)

            script.Parent.Text = "Cooldown: "..i

            if script.Parent.Text == "Cooldown: 0" then
                  script.Parent.Text = "Spawn"
            end
        end
    wait(cooldown)
    debounce = false
    end
end

Answer this question