This is my first post on resources. and I’m not good at English Sorry for my grammar
I call it Debounce
You can write any cooldown on easy way!
Functions
-
Debounce:NewPrivate()
Return new table of Debounce -
Debounce:IsCooldown(n : Name)
Return Is the given name a cooldown? -
Debounce:SetCooldown(n : Name,t : Tick(), d : Duration)
Set cooldown(d : Duration) to name, tick() for starting time
If the tick is zero/duration, Old tick/duration will be used instead.
This cooldown using tick() to cooldown thing that you giving.
Main code
local passedTime = tick() - 5 -- previous 5 second tick()
local eclapsed = tick() - passedTime -- result is 5
if eclapsed >= 5 then -- time is passed 5 seconds
-- do smth.
end
Simple :
local Debounce = require(...) -- location
-- on sharing(global)
local Cooldown = Debounce:IsCooldown("mouseclick") -- check is mouse click cooldown
if Cooldown then
Debounce:SetCooldown("mouseclick",tick(),1) -- set cooldown to "mouseclick" 1 second
end
-- for private or non-singleton (create new Debounce table)
local MouseCooldowns = Debounce:NewPrivate()
local Cooldown = MouseCooldowns:IsCooldown("mouseclick")-- same as Debounce but will checking on MouseCooldowns's Table only
if Cooldown then
MouseCooldowns:SetCooldown("mouseclick",tick(),1) -- set cooldown to "mouseclick" 1 second -- this too!
end
Source :
local Debounce = {
cooldownData = {}
}
function Debounce:NewPrivate()
local privateInstance = {
cooldownData = {}
}
setmetatable(privateInstance, { __index = Debounce })
return privateInstance
end
function Debounce:New(n)
self.cooldownData[n] = {
t = tick(),
d = 0
}
return self.cooldownData[n]
end
function Debounce:IsCooldown(n)
local dt = self.cooldownData[n] or self:New(n)
if tick() - dt.t >= dt.d then
return false
else
return true
end
end
function Debounce:SetCooldown(n, t, d)
if t then
self.cooldownData[n].t = t or tick()
end
if d then
self.cooldownData[n].d = d
end
end
return Debounce
Sorry for some of the words I used. I’m not a pro-developer.