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

How to add a debounce (cooldown) to a Text Button?

Hello,

I am working on a gui based game. I want a gui button to have a cooldown after being pressed.

Here is what I tried (you can ignore most of it, I'm just concerned about the debounce).

function clicked(plr)
     debounce = true
	if debounce == true then
		debounce = false
		local player = game.Players.LocalPlayer
		local edulvl = player.Education.Value
		local x = game.ReplicatedStorage.Work:InvokeServer(edulvl)
		local y = text(edulvl)
		script.Parent.Parent.Parent.Status.Visible = true
		script.Parent.Parent.Parent.Status.TextLabel.Text = (y.." "..x.." dollars")
		wait(5)
		script.Parent.Parent.Parent.Status.Visible = false
		debounce = true
	end
end

I thought I had my debounce set up correctly, but it doesn't seem to work.

Help would be appreciated

2 answers

✓ Accepted answer

Define debounce outside the click function because you are basically resetting the debounce, setting it to true upon clicking the button, thus removing the cooldown

	local debounce = true
	function clicked(plr)
		if debounce == true then
			debounce = false
			local player = game.Players.LocalPlayer
			local edulvl = player.Education.Value
		        local x = game.ReplicatedStorage.Work:InvokeServer(edulvl)
		        local y = text(edulvl)
		        script.Parent.Parent.Parent.Status.Visible = true
		        script.Parent.Parent.Parent.Status.TextLabel.Text = (y.." "..x.." dollars")
		        wait(5)
		        script.Parent.Parent.Parent.Status.Visible = false
		        debounce = true
		    end
		end

1 comment

0

Thank you for your response

Remove the debounce = true on line 2, and define it outside the function. You are resetting the debounce every time you click it which essentially counteracts the point of the debounce.

local debounce = true
local function clicked(plr)
	if debounce then
		debounce = false

		-- code goes here

		debounce = true
	end
end

Hope this helps! :)

4 comments

0

Hello and thank you for your reply. Doing that would cause the debounce variable to become undefined, breaking the whole script.

0

You have to define it outside the function.

0

Thank you for your responses, you have been helpful.

0

ACTUALLY, that would not break the whole entire script. If Debounce is equal to a nil value, a blank value. Then saying if debounce then is completely useless. In this case, he's defining the local variable "Debounce" with a boolean value of true, and hes stating if debounce which is the same as saying if debounce == true then.

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