Improving this Cooldown module

I made this cooldown module, i think its realy nice but hey im still learning so tell me, is there something i should change/improve and how?

local Cooldowns = {}
local CooldownList = {
	["Dash"] = 1.5,
	["Lean"] = 0.2,
	["LightAttack"] = 0.3,
	["Block"] = 0.2,
}

function roundNumber(num, numDecimalPlaces)
	return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num))
end

function Timer(Table,Cooldown)
	local Time = tick()
	repeat wait() until roundNumber((tick() - Time),1) == Table[Cooldown]

	Table[Cooldown] = nil
end

local PlayersCooldown = {}

function Cooldowns:AddCooldown(Player,Cooldown)
	if not PlayersCooldown[Player.Name] then
		PlayersCooldown[Player.Name] = {}
	end

	PlayersCooldown[Player.Name][Cooldown] = CooldownList[Cooldown]
	local thread = coroutine.create(Timer)

	coroutine.resume(thread,PlayersCooldown[Player.Name],Cooldown)
end

function Cooldowns:CheckCooldown(Player,Cooldown)
	if not PlayersCooldown[Player.Name] then
		return false
	end
	if PlayersCooldown[Player.Name][Cooldown] then
		return true
	else
		return false
	end

end


return Cooldowns

you should have spaces after commas

this can be one line

return PlayersCooldown[Player.Name][Cooldown] ~= nil

use task.wait() instead of wait()

hope this helps!

Its clear for all the other stuff but what about that task.wait() thingy? can you explain a bit better

yes I can
the task library is fairly new and it replaces spawn(), delay(), and wait()
that means they are getting deprecated and the new functions are task.spawn(), task.delay(), and task.wait()

more information on the library here

EDIT: task.defer() as stated is more of a replacement for spawn() and not task.spawn()

task.spawn() is related to fastSpawn

1 Like