Cooldown Module

Hey developers.

I am bringing out a simple utility module that probably has already been made before but I think it’s pretty neat and easy to use.

Pop this item wherever you want in your game and require it into a script.

local Module = require(path.to.module)
local Cooldown = Module.new(5) --Module.new(seconds: number)

Cooldown:Cooloff()

print(Cooldown:IsActive()) -- true

task.delay(5, function()
    print(Cooldown:IsActive()) -- false
end)

It’s pretty self explanatory on how to use it and the script is easy to read as long as you’re familiar with Lua Classes.

Thanks :slight_smile:

Raw code:

--Created by dayflare 02/11/2023.

local Cooldown = {}
Cooldown.__index = Cooldown

function Cooldown.new(Seconds: number)
	local self = setmetatable({}, Cooldown)
	self.Seconds = Seconds
	self.__start = nil

	return self
end

function Cooldown:Cooloff()
	self.__start = os.time()
end

function Cooldown:GetElapsedTime()
	return os.difftime(os.time(), self.__start)
end

function Cooldown:IsActive()
	if not self.__start then
		warn("Please initialize the cooldown with Cooldown:Cooloff()")
		return false
	end
	
	return self:GetElapsedTime() < self.Seconds
end

function Cooldown:Destroy()
	self.__start = nil
end

return Cooldown
1 Like