I tried to making a thing where when you click a button it makes your clicks leaderstats go up by 1. I can click fast, so i wanted a cooldown on 1 or 0.5 seconds. Here is my script:
for number = 1, 1 do script.Parent.MouseButton1Click:Connect(function() game.Players.LocalPlayer.leaderstats.Clicks.Value = game.Players.LocalPlayer.leaderstats.Clicks.Value + 1 end) wait(1) end
Can you tell me why there is no cooldown? Thanks!
I recommend using a debounce instead. A debounce is like a toggle of whether you should do this or not. It is crucial if you want to set up a cooldown.
The reason why your script isn't working is because you're only connecting MouseButton1Click
every 1 second. Connecting the event makes it so that the function you connected to the event will call whenever the event is fired.
Normal Script inside the button:
local button = script.Parent local player = button:FindFirstAncestorOfClass("Player") repeat player = button:FindFirstAncestorOfClass("Player") task.wait() until player ~= nil local debounce = true button.MouseButton1Click:Connect(function() if debounce then -- if debounce is true debounce = false local leaderstats = player:WaitForChild("leaderstats") local Clicks = leaderstats:WaitForChild("Clicks") Clicks.Value += 1 task.delay(1, function() -- after 1 second, debounce will turn true debounce = true end) end end)
Thanks @xInfinityBear for the suggestion to not use RemoteEvents!