Debounce Not Working

But your code yields the code, which is unnecessary, all mine does is check the delta time. os.clock() might be a global variable, but the difference between the 2 is very small. Also, os.clock() will actually be more accurate. (sorry for bad grammar)

Well whatever then just wait for the owner of the post to mark one of it as solution

1 Like

Remove ā€œlocalā€ from ā€œdebounceā€ and the script will work as expected.

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 = false
local cooldown = 0.5

script.Parent.MouseButton1Click:Connect(function()
	if debounce then return end
	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
		
		return
	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
	
	debounce = false
	delay(cooldown, function() debounce = true end)
end)

What is going on here? So many different people are posting overly complicated methods of stopping and starting debounces.

This is typically how I do it. Not sure if it would work for your exact scenario, but setup a debounce system such as this:

local debounce = false

if not debounce then -- if the "debounce" variable is false, then continue.
    debounce = true -- Make it so the script won't run again by setting the debounce to true

    -- Your code that you wrote would generally go here. It works for pretty much every script I've done that needed a cooldown.

    task.wait(3) -- you can put any number here. The number you put will act as the cooldown.

    debounce = false -- Make it so the script can run again by setting it to false after the task.wait()
end

could also use deltaTime for better accuracy and consistency

Isnā€™t deltaTime based on CPU clock? I never really found any good use for it.

task.wait() is a heartbeat based version of wait(). Itā€™s pretty much better and more accurate according to Roblox engineers.

Found on the official announcement thread here.

Pretty sure deltaTime is also more confusing and doesnā€™t seem necessary at all for a debounce.

Itā€™s not confusing at all, the code is just asking ā€œHey, has it been {cooldown} seconds since you clicked?ā€, and it doesnā€™t need to yield the code. But youā€™re also not wrong, task.wait() is more accurate and its based on heartbeat.

1 Like

I think in my off time Iā€™ll learn more about deltaTime. As for the OP, they should just stick to what is simple and use task.wait(). Although it may be way more accurate then task.wait(), it still seems a bit redundant to do it that way. Just my recommendation though.

1 Like

I mean, its just a simple debounce usage so no problem at all.

1 Like
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 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 should most likely work

1 Like

there are unnecessary lines of code. Just put the debounce outside the if statements.

1 Like

just try my solution. yall are trying to use things like os.clock() and os.time() when in fact, the correct solution is the easiest, and most performance friendly.

why will u make a global variable for a debounce? Also, os.clock() isnā€™t wrong, itā€™s just checking for the deltaTime instead of yielding which is unnecessary.

1 Like

Why not make it global? If you were to create it inside the Mouse click function it would most likely mess the script up, unless you have something else in mind.

You donā€™t need to make a global variable as you can just define it as a local variable outside the event listener.

1 Like

Why this topic became a Debounce code showcase?


Use prints to detect where is the problem. The best I can say since everyone already showed lots of solutions.

2 Likes

global variable of the script. not from the server. at least try my solution.

use this script in first.

using local

local debounce = false
script.Parent.Touched:Connect(function(instance)
	if instance.Parent:FindFirstChild("Humanoid") and debounce == false then
	    debounce = true
		print("debounce false")
	task.wait(5)
		debounce = false
	end
end)

Correct way

debounce = false
script.Parent.Touched:Connect(function(instance)
	if instance.Parent:FindFirstChild("Humanoid") and debounce == false then
	    debounce = true
		print("debounce false")
	task.wait(5)
		debounce = false
	end
end)

if there is a ā€œlocalā€ in the debounce, the part would not have a real debounce, and would print every time the player touched the part. while without ā€œlocalā€, debounce is enabled for the script, not a local variable(only 1 ā€œsessionā€) and only print 1 time.

Thatā€™s what Iā€™m trying to show now. There are more than 35 messages from people trying to solve the problem. probably they didnā€™t even experience the problem themselves.

Using local assigns the variable to a value in the current scope. Unless youā€™re using the variable outside of itsā€™ assigned scope, thereā€™s no difference between using local or not in this situation.

1 Like