Cooldown doesn't work

i wanna add cooldown but it doesn’t work ! i am making a rng game coz i finished my other game and the cooldown doesn’t work as u can see i made “wait(5)” in the end
script:

local btn = script.Parent
local txt = btn.Parent.Background:WaitForChild("TextLabel")
local CC = workspace.CoinCollection
local x = game.Players.LocalPlayer:WaitForChild("leaderstats")
local tokens = x:WaitForChild("Tokens")

btn.MouseButton1Click:Connect(function()
	txt.Parent.Visible = true
	local rng = Random.new()
	local num = rng:NextInteger(1, 100)
	txt.Text = tostring(num)
	tokens.Value = tokens.Value + num
	CC:Play()
end)

wait(5)
1 Like

That’s because you’re not placing this cooldown in the actual MouseButton1Click event, so the script just wait for 5 seconds once, Try placing this cooldown into the MouseButton1Click event like this:

local btn = script.Parent
local txt = btn.Parent.Background:WaitForChild("TextLabel")
local CC = workspace.CoinCollection
local x = game.Players.LocalPlayer:WaitForChild("leaderstats")
local tokens = x:WaitForChild("Tokens")

local debounce = false

btn.MouseButton1Click:Connect(function()
 if not debounce then
    debounce = true
	txt.Parent.Visible = true
	local rng = Random.new()
	local num = rng:NextInteger(1, 100)
	txt.Text = tostring(num)
	tokens.Value = tokens.Value + num
	CC:Play()
    wait(5)
    debounce = true
  end
end)

doesn’t work
111111111111111111111111

1 Like

This should work. If it doesn’t, can you please respond with some sort of error message?

no errors, the frame doesn’t get invisible after 2 seconds (figured it out)

thats because you never set it to false

i suppose you are reffering to this frame?

guys i figured it out dw
1111111111111111111111111

i forgot to put it thoguht i didn’t that’s why ik it xd mb

1 Like