Cooldown
Cooldown is a module that helps with creating debounces and, as the name implies, cooldowns. Basic Usage:
local Cooldown = require(Path.Cooldown)
local DebounceTime = 5
local Debounce = Cooldown.new(DebounceTime)
Debounce:Run(function()
print("Ran!")
end)
Debounce:Run(function()
print("Ran Again!")
end)
-- Output:
-- Ran!
Properties
Time
Cooldown.Time: numberThe time property signifies how much time is needed to wait before using :Run()
An example would be:
local Cooldown = require(Path.Cooldown)
local Debounce = Cooldown.new(5) -- The first parameter is the Time
-- Can be changed with Debounce.Time = 5
Debounce:Run(function()
print("This will run") -- prints
end)
Debounce:Run(function()
print("This won't run") -- won't print because the debounce hasn't finished waiting 5 seconds
end)
note
Calling :Run() when the debounce isn't ready won't yield.
AutoReset
Cooldown.AutoReset: booleanWhen AutoReset is on, the debounce will reset after a successful Run() call.
An example would be:
local Cooldown = require(Path.Cooldown)
local Debounce = Cooldown.new(5)
Debounce.AutoReset = false
-- Keep in mind you can also set the AutoReset by the second parameter in the constructor: Cooldown.new(5, false)
Debounce:Run(function()
print("This will run") -- prints
end)
Debounce:Run(function()
print("This will still run") -- still prints because AutoReset is false and the debounce did not reset
end)
Debounce:Reset() -- Reset the debounce
Functions
Simple
Cooldown.Simple(Time: number,Function: (T...) → (R...)) → (T...) → (boolean,R...)Adds a cooldown to a function. It returns a copy of the function that always returns a boolean first, which is true if the function ran or false if the function didn't run because of the cooldown.
new
Cooldown.new(Time: number,--
The time property, for more info check the "Time" property.
AutoReset: boolean?--
Sets the AutoReset value to the boolean provided, please refer to Cooldown.AutoReset
) → CooldownTypes
interface Cooldown {Time: number--
The time of the debounce
LastActivation: number--
The last time the debounce reset
AutoReset: boolean--
Whether or not the debounce should reset after running.
}Returns a new Cooldown.
Errors
| Type | Description |
|---|---|
| "No Time" | Happens when no Time property is provided. |
Reset
Cooldown:Reset(Delay: number?--
The amount of delay to add to the Time
) → number--
The cooldown time + delay.
Resets the debounce. Just like calling a sucessful :Run() with AutoReset set to true If a delay is provided, the debounce will be delayed by the provided number. A delay will only last once. An example would be:
local Cooldown = require(Path.Cooldown)
local Debounce: Cooldown = Cooldown.new(2)
Debounce.AutoReset = false
Debounce:Run(function()
print("This will run") -- prints
end)
Debounce:Reset(1) -- We reset it and delay it by 1
Debounce.OnReady:Wait() -- We wait 3 seconds instead of 2, because we delay it by 1.
-- You can think of delaying as adding time + delay which would be 2 + 1 in our case
-- Delaying will not change the time.
Debounce:Run(function()
print("This will run") -- will print because the :Run will be ready.
end)
Activate
Cooldown:Activate() → ()Makes the cooldown ready again.
Run
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsCooldown:Run(Callback: (Args...) → (),...: any) → booleanRuns the given callback function if the passed time is higher than the Time property. If AutoReset is true, it will call :Reset() after a successful run.
RunIf
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsCooldown:RunIf(Predicate: boolean | () → boolean,Callback: (Args...) → (),...: any) → booleanIf the given Predicate (The First parameter) is true or returns true, it will call :Run() on itself.
An example would be:
local Cooldown = require(Path.Cooldown)
local Debounce = Cooldown.new(5)
Debounce.AutoReset = false
Debounce:RunIf(true, function()
print("This will run") -- prints
end)
Debounce:RunIf(false, function()
print("This will not run") -- does not print because the first parameter (Predicate) is false.
end)
RunOrElse
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsCooldown:RunOrElse(Callback: () → (),Callback2: () → ()) → ()if the :Run() will not be successful, it will instead call callback2. This won't reset the debounce.
An example would be:
local Cooldown = require(Path.Cooldown)
local Debounce = Cooldown.new(5)
Debounce:RunOrElse(function()
print("This will run") -- prints
end, function()
print("This will not print") -- doesn't print because the :Run() will be successful.
end)
Debounce:RunOrElse(function()
print("This will not run") -- does not print because the debounce hasn't finished waiting.
end, function()
print("This will run") -- will print because the :Run() failed.
end)
Wrap
Cooldown:Wrap(Function: (T...) → R...) → WrapFuncReturn<T...,R...>Wraps a cooldown class to a function (similar to Cooldown.Simple). It returns a Cooldown class that when called, it will call Cooldown:Run() on the given function. When calling the cooldown class, the first return will always be a boolean before the returns. If the function succesfully runs, the boolean will be true.
IsReady
Cooldown:IsReady() → booleanReturns a boolean indicating if the Cooldown is ready to :Run().
GetPassed
Cooldown:GetPassed(Clamped: boolean--
If this is true, it will use math.clamp to make sure the value returned is min 0 and max the time.
) → numberReturns a boolean indicating the passed time since the last :Run().
GetAlpha
Cooldown:GetAlpha(Reversed: boolean--
If true, will return alpha as 0 if fully ready to :Run() instead of 1.
) → numberReturns the time before the :Run() is ready in a value between 0-1.
Destroy
Cooldown:Destroy() → ()Destroys the Cooldown.