Like when I use it in loop or other then it will crash
while
— there happens a lot untill the debounce is false and the wait is finish
wait()
debounce = false
Like when I use it in loop or other then it will crash
while
— there happens a lot untill the debounce is false and the wait is finish
wait()
debounce = false
Can I use ur function with a if statement will it work?
It should, but it depends on your current implementation.
doStuff already does the if stuff
what do you mean you cant test it out in ur Studio??
Cooldown = 3
RunService.....
if debounce == true then
print("hey") -- prints(10000 times
wait(CoolDown)
debounce = false
end
end)
what???
Your method with debounce and wait() isnt a good Method
It will be stuck in Roblox Studio and crash it
did you put it in a while loop?
Again, your method is not working cuz I want a cooldown without any chrashes. If I would use it in a while loop or in a RunnService it will crash cuz untill the wait(CoolDown) ends it will print too many times “hey”
This is why I would not prefer
if pressed == true then
wait()-- time you want to wait
pressed = false
end
wait()
has its place, but it is thread-blocking and messy. I still would recommend you go with my original advice. Whenever someone tries to use something with a cooldown, check if enough time has passed for it to be allowed. This way it does not block any threads and is only called when it is needed.
There are a couple ways to do it but the best way is with the method @SuchASaltyLemon used.
You’ll want to do fully on the server so it’s anti-exploitable. This way basically compares the last timestamp to how much time has passed instead of using instances or waits. Another thing is to use os.clock() because from what I’ve heard it is the best function for timestamps.
local cooldown = 5
local cooldowns = {}
local function CreateCooldown(player)
cooldowns[player.UserId] = os.clock()
end
local function CanUse(player)
local last = cooldowns[player.UserId]
if os.clock() - last >= cooldown then
return true
end
return false
end
Tables are used so that you can store multiple player cooldowns.
Also, an even better method would to use module scripts for the cooldown table so it is accessible by multiple server scripts.
Edit: You’ll want to check if the player is actually in the table first before setting their cooldown, or else you will have an error. You can use a Player Added function and add their UserId in that way, or you can check if it is nil when you check their cooldowns and if it is, add their UserId.
this is not working maybe because I am using it in RunService too
I looked at your first post and your variable names are a bit funny. You used the method that @Fusionet and I recommended, but you have previous
as the first variable, but then later Previous
debounce = false
if not debounce then
debounce = true
print("hey")
wait(cooldown)
debounce = false
end
This method is not as reliable for a couple reasons.
If used on the client it is easily exploitable. Also, the wait() function is sometimes never accurate and can actually end up waiting much more than it is supposed to. For this reason the timestamp method with os.clock() is much better.
guess the recourses i am using are a bit outdated lol
I don’t use the cooldown you mentioned, but instead use the os.time() method. Is there a difference? Does it increase performance by limiting client & server, running the cooldown on both instead of firing an event every frame.