Debounce Not Working

Hey there,
I got this script here that clicks coins to player whenever they click but I want to add a cooldown on it, so I added debounce but it isnt working for some reason, I got no errors nothing. So if you can see whats wrong with it or how I could script it better please feel free to reply! :))

local plr = script.Parent.Parent.Parent.Parent.Parent
local getMulti = require(game.ServerScriptService.Modules.PetHandler.PetMultiplier)
local shash = 0
local plus = getMulti:GetPetMultipliers(plr, "Coins")
local debounce = true
local cooldown = 0.5

script.Parent.MouseButton1Click:Connect(function()
	if debounce == true then
	local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
	if hasShinyBoost then
	plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
	else
		plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
			debounce = false
			wait(cooldown)
            debounce = true
		end
	end
end)
1 Like

Try this:

local plr = script.Parent.Parent.Parent.Parent.Parent
local getMulti = require(game.ServerScriptService.Modules.PetHandler.PetMultiplier)
local shash = 0
local plus = getMulti:GetPetMultipliers(plr, "Coins")
local debounce = true
local debounce2 = false
local cooldown = 0.5

script.Parent.MouseButton1Click:Connect(function()
	if debounce == true then
      debounce = false
		local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
		if hasShinyBoost then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		else
			if not debounce2 then
			debounce2 = true
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
			debounce = false
			wait(cooldown)
				debounce = true
				debounce2 = false
			end
		end
	end
end)
1 Like

Not working with no errors sorry ://

Hello, I think it should work now.

Hey, so its working but its not at the same time, You can click it once but then it wont allow you to click it again at all. Thanks for your help.

The reason is because the debounce you placed in the script is wrong. First, use if statement to check if the debounce is true. Then, right after that if statement, set the debounce to the opposite of its current value, then do your main code, once it’s done, wait for a certain amount of seconds as your cooldown, then set the debounce back to its original value.

1 Like

So this should work:

local plr = script.Parent.Parent.Parent.Parent.Parent
local getMulti = require(game.ServerScriptService.Modules.PetHandler.PetMultiplier)
local shash = 0
local plus = getMulti:GetPetMultipliers(plr, "Coins")
local debounce = true
local debounce2 = false
local cooldown = 0.5

script.Parent.MouseButton1Click:Connect(function()
	if debounce == true then
		debounce = false
		local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
		if hasShinyBoost then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		else
			if not debounce2 then
				debounce2 = true
				plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
				plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
				debounce = false
				wait(cooldown)
				debounce = true
				debounce2 = false
			end
		end
	end
end)

Why is this debounce inside the if statement? It should be at the scope of the function and not the if statement.

Im not sure, The guy above helped me.

You shouldn’t, because if you do this, your if statement will screw up the whole thing by not setting the debounce back to true if the first if statement passed.

1 Like

That would explain why its working then but how do I fix it?

Just

event.Event:Connect(function()
    if debounce then
        debounce = not debounce 
        — your main code

        wait(cooldown)
        debounce = not debounce
    end
end
1 Like

Hey, maybe you can just use os.clock(), easier to use and accurate

--your variables here

local last = 0
local cooldown = 0.5

script.Parent.MouseButton1Click:Connect(function()
    if os.clock() - last >= cooldown then
       last = os.clock()
       --your code here
    end
end)

Here is my script now but it just clicks once and doesnt work after that:

local plr = script.Parent.Parent.Parent.Parent.Parent
local getMulti = require(game.ServerScriptService.Modules.PetHandler.PetMultiplier)
local shash = 0
local plus = getMulti:GetPetMultipliers(plr, "Coins")
local debounce = true
local debounce2 = false
local cooldown = 0.5

script.Parent.MouseButton1Click:Connect(function()
	if debounce then
		debounce = not debounce
		local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
		if hasShinyBoost then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		else
				plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
				plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
				wait(cooldown)
				debounce = not debounce
			end
		end
end)

I give up explaining because you don’t really understand what I meant. Here’s the code you can copy instead.

script.Parent.MouseButton1Click:Connect(function()
	if debounce then
		debounce = not debounce
		local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
		if hasShinyBoost then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		else
				plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
				plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
			end
		end
        wait(cooldown)
        debounce = not debounce
end)

Seriously you guys all gave him the wrong code just copy and paste this code instead man

local cooldown = 3 -- change this to your cooldown time
local debounce = true
script.Parent.MouseButton1Click:Connect(function()
	if debounce then
		debounce = false
		local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
		if hasShinyBoost then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		else
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
		    plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
		end
        wait(cooldown)
        debounce = true
	end

end)

This is better because its accurate and its easier to use.
Code if its a server script:

local cooldowns = {}
local cooldown = 0.5 -- change this to your liking
local last = 0

script.Parent.MouseButton1Click:Connect(function()
	if cooldowns[plr] == nil or os.clock() - cooldowns[plr] >= cooldown then
        cooldowns[plr] = os.clock()
		local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
		if hasShinyBoost then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		else
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
		    plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
		end
	end
end)

Code if its a local script:

local cooldown = 0.5 -- change this to your liking
local last = 0

script.Parent.MouseButton1Click:Connect(function()
	if os.clock() - last >= cooldown then
        last = os.clock()
		local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
		if hasShinyBoost then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		else
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
		    plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
		end
	end
end)

Your code will be too laggy because it uses os.clock() which requires more internet data because it is global. Using a local variable is better and more efficient. This is the best way:

local cooldown = 3 -- change this to your cooldown time
local debounce = true
script.Parent.MouseButton1Click:Connect(function()
	if debounce then
		debounce = false
		local hasShinyBoost = game:GetService("MarketplaceService"):UserOwnsGamePassAsync(plr.UserId, 21797590) -- check pass
		if hasShinyBoost then
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
			plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins") * 2
		else
			plr.Stats.Coins.Value = plr.Stats.Coins.Value + getMulti:GetPetMultipliers(plr, "Coins")
		    plr.MiniStats.TotalCoins.Value = plr.MiniStats.TotalCoins.Value + getMulti:GetPetMultipliers(plr, "Coins")
		end
        wait(cooldown)
        debounce = true
	end

end)

Im sorry but how does os.clock() require more data?
But if it really is, then i recommend just switch wait(cooldown) to task.wait(cooldown)
which is based on the next heartbeat (if im correct)

os.clock() is a global variable while creating a variable using local does not require more data as it is local, not global.