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)
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.
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.
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)
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)