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

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

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.

1 comment

0

local db = false should be outside the event

2 answers

✓ Accepted answer

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)

1 comment

0

Worked!

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

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