Cooldown Module

Hey, so I’m back again with another module. A module like this one might already exist but I couldnt find any topics on this really, so I made my own. This one is for creating timed cooldowns. You can also hook your own functions onto each Tick of the cooldown and the End of the cooldown. The module can be found here, and the API documentation is down below:

API 1.0

Cooldown.new(lifetime, tickrate, tickfunc, finishfunc)

Returns a new Cooldown.
lifetime - How much time the cooldown takes.
tickrate (Optional) - How many seconds between each tick of the cooldown. The default is 1.
tickfunc (Optional) - A function connected to the cooldown. It is called each time the cooldown ticks.
finishfunc (Optional) - A function connected to the cooldown. It is called once the cooldown finishes.

Cooldown:Start()

Starts the cooldown.
void - This function takes in no parameters.

Cooldown:Stop()

Stops the cooldown if it is already started.
void - This function takes in no parameters.

Cooldown:IsRunning()

Returns true or false depending on if the cooldown is active or not.
return - Returns self.IsRunning

Cooldown:GetTimeLeft()

Returns how much time is left before the cooldown ends.
return - Returns self.TimeLeft

Cooldown:GetMaxTime()

Returns the lifetime of the cooldown (How many seconds it was originally counting down from).
return - Returns self.Lifetime

Cooldown:Tick(func)

Binds a user-created function to the tick connection.
func - A function that will run each time the cooldown ticks.

Cooldown:Finish(func)

Binds a user-created function to the finish connection.
func - A function that will run as soon as the cooldown is finished.

Nice, but anyone can make a cooldown easily with a simple debounce so this seems a bit overkill.

Also literally no point to this method


thanks for linking CollectionService for no apparent reason

4 Likes

This is pretty cool, I’ve made something similar myself.

Code
export type Cooldown = {
	add: (any) -> (),
	has: (any) -> (boolean),
	remove: (any) -> (),
	removeAfter: (any, number) -> (),
}

local Cooldown: Cooldown = {}

function Cooldown.add(object: any)
	Cooldown[object] = true
end

function Cooldown.has(object: any): boolean
	return Cooldown[object] and true
end

function Cooldown.remove(object: any)
	Cooldown[object] = nil
end

function Cooldown.removeAfter(object: any, duration: number)
	Cooldown.add(object)
	task.delay(duration, Cooldown.remove, object)
end

return Cooldown

it’s not fully featured like yours though, I’d probably rewrite it a bit to make it more functional.

thanks for sharing your resource!

3 Likes