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
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.
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.
I mean, its just a simple debounce usage so no problem at all.
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
there are unnecessary lines of code. Just put the debounce outside the if
statements.
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.
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.
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.
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.