How to get rid of global cooldowns | How to make table of cooldowns with os.clock() or tick()

Before I get into it, first of all, why did I feel like I needed to do this? Dunno, just to help people i guess lol

Let’s begin!

Has this ever happened to you? You fire the remote which has a cooldown of per say 1 second. Within that 1 second, someone else fires but cannot execute the script. That’s because of a remote placed in Replicated Storage which makes it a global remote event meaning everyone fires 1 remote. Which is received by 1 script(I assume. Should be!!). Well, that’s a global cooldown. It looks something like this

local debounce = false
game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(plr)
	if debounce then return end -- If debounce == true then return end meaning can't run the code below if it's true!
	debounce = true
	
	print("Fired!")
	
	wait(5)
	debounce = false
end)
-- What happens here?
--[[
Well, pretty simple. The remote event recieved in this script was placed in replicated storage and couldn't be used twice within 5 seconds(Assuming there is no latency which there will be)

What does it cause?

Let's say John fires the remote and gets past debounce since it's false but then after he uses it, it's true. While the debounce is true, Jessica canno't get past the debounce within 5 seconds


]]

-- Is there a way to stop this? Yes! Just follow these instructions.

local tableOfCooldowns = {}
game:GetService("ReplicatedStorage").RemoteEvent.OnServerEvent:Connect(function(plr)
	
	if tableOfCooldowns[plr] and os.clock() - tableOfCooldowns[plr] < 5 then return end -- Return if the player is in the table AND 5 seconds has not passed since that SAME player has fired the remote event.
	tableOfCooldowns[plr] = os.clock() -- Add the player to the table with os.clock(). Which goes up every second(Maybe ms too but not sure.)
	
	print("Fired!") -- Prints "Fired!"

end)

-- What is the difference here?

--[[
We added John to the table when he fired the remote event and a cooldown for *JOHN* has started.

After John, Jessice fires within 5 seconds and could get past the cooldown.

Why is that?

Well as you see, we made a cooldown for each individual player that fires the remote event with a table. Tables are useful!
]]

Hopefully this was helpful. I’m new to dev forum, hopefully will get better :stuck_out_tongue:

This can also be used for events like Touched to damage multiple people at once without dealing too much damage than it’s supposed to!

14 Likes

Replacing os.clock() with tick() would also work but, tick() is deprecated at the moment.

I really want to ask, is this a question or a tutorial because if it’s a tutorial this topic belongs to #resources:community-tutorials category more than #help-and-feedback:scripting-support since scripting support category is for asking help about scripting.
Also nice tutorial if you meant to post it to the community tutorials category I mentioned.

2 Likes

tick() is not exactly deprecated and probably never will as it seems to be a very common usage of acquiring local time on the machine. :slight_smile:

It might end up getting deprecated at some point according to this thread:

3 Likes