Effective Server-sided cooldown

I’ve been wondering this for a while but I would like to know the best way to create the most effective server-sided cooldown that creates the least amount of lag since I heared.

     If Tab[Char] ~= true then
          Tab[Char] = true


          wait(1)
          Tab[Char] = false
     end

Causes memory leak causing delay on the server.
(I checked this out but I don’t know how effective it is Better way to make serversided cooldown system? - #2 by sleitnick)

That causes a memory leak because you never delete the reference to the character. Simply replace false with nil to solve the memory leak.

If I ever needed to make a cooldown I would use something like os.time() each time the player activated something and compare the current os.time() to the their last os.time() to get the elapsed time.

local remote = “Event” – Whatever remote you are referencing

local debounceTime = 5
local tab = {}

remote.OnServerEvent:Connect(function(player)
if not tab[player] then
tab[player] = os.time()
end

if os.time() - tab[player] >= debounceTime then
	tab[player] = os.time()
	print("it worked :3")
end

end)

Yes, the link you provided is much better than this option. You don’t want to yield when you don’t have to. You can read this: Avoiding wait() and why to understand more about why yielding is not good. Remote events also run on their own thread which means that you can be yielding on a bunch of threads. It is just cleaner to use the one you linked.

tick() is better for this situation, because of its precision. Os.time() is only down to the second, so you wouldn’t be able to use decimals. With tick(), you can.

1 Like

I mean…

Yeah.

Memory leaks are annoying to deal with.

All of the above are great and utilising traditional debounce cases will probably net you a half decent server-sided cooldown.

You’ll want to minimise the amount of load at all times.

The thread you provided is perfectly appropriate and I’d highly recommend you fork that kind of methodology and put it into your own code with your own use cases.

The only reason why that will cause a memory leak is because your setting the value to false, instead set to nil so the reference properly gets removed:

if (not Table[Character]) then
    Table[Character] = true
    wait(1)
    Table[Character] = nil --removing the reference thus stopping memory leaks
end

This solution will cause a memory leak when the player disconnects, the original post method is perfectly fine just set the reference to nil.

Thanks for all your help I’ll definitely be using this new information since you all had good solution I can’t really pick one.