Help adding debounce and countdown

Hey, I need help adding debounce to this script, so everytime you click the TextButton, it leaves you with a countdown from 5 to 0 seconds, until you can click it again. It then returns to the normal text, β€œ:money_with_wings: Gamble :money_with_wings:”.

The textbutton in here is already set as a variable, and it’s called TextButton

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local coins = game.Players.LocalPlayer.leaderstats.Scamollars
	local coinsn = game.Players.LocalPlayer.leaderstats.Scamollars.Value
	local randomnumber = math.random(1, 3)
	local eventwin = game.ReplicatedStorage.Gamble.GambleWin
	local eventlost = game.ReplicatedStorage.Gamble.GambleLost
	local plr = game.Players.LocalPlayer
    local TextButton = script.Parent.Parent.TextButton ------------ DEVFORUM SUPPORT
	local EnterAmount = script.Parent.Parent.TextBox 
	local EnterAmountTxt = script.Parent.Parent.TextBox.Text
    local Coins = game.Players.LocalPlayer.leaderstats.Scamollars
	local str = EnterAmountTxt
	local num = tonumber(str)
	print(num)
    local str1 = coinsn
	local coinnum = tonumber(str1)
	print(coinnum)
	
	if num <= 0 then
		script.Parent.Loss:Play()
		script.Parent.Parent.Parent.Menu.Status.TextColor3 = Color3.new(1, 0, 0)
		script.Parent.Parent.Parent.Menu.Status.Text = "❌ Insert a value, higher than 1+!"
		wait(5)
		script.Parent.Parent.Parent.Menu.Status.Text = ""

	return end
	
	
	if num == nil then
		script.Parent.Loss:Play()
		script.Parent.Parent.Parent.Menu.Status.TextColor3 = Color3.new(1, 0, 0)
		script.Parent.Parent.Parent.Menu.Status.Text = "❌ Enter an amount!"
		wait(5)
		script.Parent.Parent.Parent.Menu.Status.Text = ""
		
		
return end

	if num > coinnum then
		script.Parent.Parent.Parent.Menu.Status.TextColor3 = Color3.new(1, 0, 0)
		script.Parent.Parent.Parent.Menu.Status.Text = "❌ You don't have that much coins!"
		script.Parent.Loss:Play()
		wait(5)
		script.Parent.Parent.Parent.Menu.Status.Text = ""
		
		return
	else
		--Put here what will happen if they have enough.
		print("ENOUGH COINS.")


		if coins.Value <= 0 then
			script.Parent.Parent.Parent.Menu.Status.TextColor3 = Color3.new(1, 0, 0)
			script.Parent.Parent.Parent.Menu.Status.Text = "❌ Not enough coins!"
			script.Parent.Loss:Play()
			wait(5)
			script.Parent.Parent.Parent.Menu.Status.Text = ""

			return end

		print(randomnumber)

		if randomnumber == 1 then
			print("You've won!")
			script.Parent.Parent.Parent.Menu.Status.TextColor3 = Color3.new(0.333333, 1, 0)
			script.Parent.Parent.Parent.Menu.Status.Text = "πŸŽ‰ You've won!"
			script.Parent.Win:Play()
			 
			
			local str = EnterAmountTxt
			
			local num = tonumber(str)
			print(num)
			
			eventwin:FireServer(plr, num)

			wait(5)
			script.Parent.Parent.Parent.Menu.Status.Text = ""


		else
			print("You've lost!")
			script.Parent.Parent.Parent.Menu.Status.TextColor3 = Color3.new(1, 0, 0)
			script.Parent.Parent.Parent.Menu.Status.Text = "❌ You've lost!"
			script.Parent.Loss:Play()
			
			eventlost:FireServer(plr, num)

			
			
			wait(5)
			script.Parent.Parent.Parent.Menu.Status.Text = ""

		end
	end
end)

Your code is a tad bit messy for me (no offense), so I’ll just show you a basic cooldown function that you can put into your script:

for remainingTime = 5,1, -1 do 
    print(remainingTime)
    wait(1)
end
1 Like

this wasn’t helpful to me, considering i’ve never done this

first, add a new variable called db (short for debounce)

db = false

then, on top of the function, we will make sure if db is false, and also set it to true so the function wont go any further. after the function, add a wait() and then make db false.

script.Parent.MouseButton1Click:Connect(function()
 if db == false then
  db = true
  --rest of code
  wait(2)
  db = false
 end
end)
1 Like

I thought you wanted to have a countdown show for your text button, which could be done with that for loop. It’s almost like a regular loop, except it goes from 5 to 1 instead of 1 to 5.

You probably first want to declare an additional debounce variable at the top of your script and use it this way inside the MouseButton Click:

local debounce = false

script.Parent.MouseButton1Click:Connect(function()
    if debounce then return end debounce = true
    local player = game.Players.LocalPlayer
    local coins = game.Players.LocalPlayer.leaderstats.Scamollars
    local coinsn = game.Players.LocalPlayer.leaderstats.Scamollars.Value
.....
.....

After this, make sure you add this line:

debounce = false

before each of your return keywords!

So for example:

    if num <= 0 then
		script.Parent.Loss:Play()
		script.Parent.Parent.Parent.Menu.Status.TextColor3 = Color3.new(1, 0, 0)
		script.Parent.Parent.Parent.Menu.Status.Text = "❌ Insert a value, higher than 1+!"
		wait(5)
		script.Parent.Parent.Parent.Menu.Status.Text = ""
        debounce = false
	return end

but for all your possible scenarios.

Hope this helps, let me know if anything is unclear!