Roblox Cooldown Handler Module

Hello, everyone! I thought I would share this simple module I created a long time ago which helps manage skill cooldowns on the server side. It requires knowledge on Data Tables, Remote Events, Module Scripts, and Control Flow.

The guide I wrote isn’t that good, but I just wanted to post this for anyone who might find it useful somehow.

Roblox Cooldown Handler Guide

3 Likes

Can I post a different & shorter approach

yeah sure, that would be cool.

1 Like
local cooldowns = {}
local active = {}

function cooldowns:Set(cd, _time)
	cooldowns:Clr(cd)
	active[cd] = task.delay(_time, function()
		active[cd] = nil
	end)
end

function cooldowns:Has(cd)
	return active[cd]
end

function cooldowns:Clr(cd)
	if (active[cd]) then
		task.cancel(active[cd])
	end
end

return cooldowns

this is what i use for my game (for the client side- the server side i havent done yet but would creating cooldown ‘objects’ which have their own active cooldowns array and assigning it to each player)

2 Likes

this is my server implementation i havent tested it yet

local cooldowns = {}
cooldowns.__index = cooldowns

function cooldowns.New()
	return setmetatable({}, cooldowns)
end

function cooldowns:Set(cd, _time)
	cooldowns:Clr(cd)
	self[cd] = task.delay(_time, function()
		self[cd] = nil
	end)
end

function cooldowns:Has(cd)
	return self[cd]
end

function cooldowns:ClrAll()
	for k in next, (self) do
		task.cancel(self[k])
		self[k] = nil
	end
end

function cooldowns:Clr(cd)
	if (self[cd]) then
		task.cancel(self[cd])
	end
end

function cooldowns:Destroy()
	self:ClrAll()
	setmetatable(self, nil)
end

return cooldowns
3 Likes

alright cool thx, I’ll get to testing in my system as well and see if it works!

1 Like

np
scrap the client side one
on both side just make a cooldown object and destroy & recreate it every time the players character loads cuz every time the player respawns all their cooldowns would be gone which is what you want

id prefer having the cooldowns in the server side because exploiters can access them if they are created and destroyed client side.

you need cooldowns on both ends for when the client does x action and the server does x action

the way I have mine set-up I don’t have cooldowns on the client.