Better way to create cooldowns?

Basically when I need to add cooldowns I normally use TweenService on a Value so I can Play and Stop the cooldown whenever I want. However it feels a bit weird so I was just wondering if there’s a better way to add cooldowns.

debounce value

local debounce = false
local function dosomething()
   if debounce == false
      print("hi")
      debounce = true
      wait(5)
      debounce = false
   end
end

This is even worse than the original idea. You’re using wait() and you cannot stop it, in case something allows so.

2 Likes

there are many ways to use a debounce value, it doesnt have to be a wait

im just putting an example

Now make an example where you would have multiple cooldowns and be able to cancel any of them (such as games containing skills, for example).

1 Like

coding in a text box is hard

just use bindable events that are hooked up to the main debounce value itself

or manually change the debounce value in the code

the wait function only applies to one thread, if you used the wait without the debounce it would run in a time smaller than 5 seconds with no problem

I dont think you got my point… I want to know if there’s another way of creating cooldowns that I can stop when needed, one of them is TweenService (the one I use normally) but I’m not sure if there’s a better way to do it so that’s why I’m asking

1 Like

You can make a folder inside serverstorage and put cooldowns as a number value

This is how I’d go about it, making it robust and easy to use:

local CooldownSystem = {}
CooldownSystem.playersOnCooldown = {}

function CooldownSystem:SetCooldown(player, cooldownTime)
    self.playersOnCooldown[player.UserId] = tick() + cooldownTime
end

function CooldownSystem:IsOnCooldown(player)
    local cooldownEnd = self.playersOnCooldown[player.UserId]
    if cooldownEnd then
        if tick() > cooldownEnd then
            self.playersOnCooldown[player.UserId] = nil
            return false
        else
            return true
        end
    else
        return false
    end
end


function CooldownSystem:GetRemainingCooldown(player)
    local cooldownEnd = self.playersOnCooldown[player.UserId]
    if cooldownEnd then
        local remaining = cooldownEnd - tick()
        if remaining <= 0 then
            self.playersOnCooldown[player.UserId] = nil
            return 0
        else
            return remaining
        end
    else
        return 0
    end
end


local player = game.Players.LocalPlayer  -- Or any other player you'd like to set/check cooldown for

-- Set a cooldown for a player for 10 seconds
CooldownSystem:SetCooldown(player, 10)

-- Check if a player is on cooldown
local onCooldown = CooldownSystem:IsOnCooldown(player)
print(onCooldown)  -- This will print 'true' if the player is on cooldown and 'false' otherwise.

-- Get remaining cooldown time for a player
local remainingTime = CooldownSystem:GetRemainingCooldown(player)
print(remainingTime)  -- This will print the number of seconds left in the cooldown.

Note that this is a basic implementation, use a ModuleScript and require it, and expand on it as needed. You should add error handling, type checking, etc… but you won’t learn if just dish out a more industrial solution.

Hope it helps.

2 Likes

Maybe if you show your example of using TweenService you would get some more suggestions. I don’t get how you are using TweenService to do this and what it does for you.

Can you give a detailed example of a practical application of this.

local cooldownTime = 5 
local isCooldownActive = false

local function startCooldown()
    isCooldownActive = true
    local endTime = os.time() + cooldownTime
    while os.time() < endTime do
        wait(1) 
        if not isCooldownActive then
            break 
        end
    end
    isCooldownActive = false
    print("Cooldown has ended!")
end

startCooldown()

wait(10)
isCooldownActive = false
1 Like

The other responses work but its honestly much easier to use tick() which returns the current time in seconds since a date from like 1970.

So when checking the cooldown just do this

if (tick() - LastTick) >= cooldowntime then else return end

set LastTick to the tick() when you do the action that needs the cooldown

local debounce = false

--inside the function where u want the cooldown to work
if debounce then return end
debounce = ture

--your logic here

task.wait(delay)
debounce = false
local cooldown: number = 0
local cooldownTime: number = 5 --set to cooldown you want

local cooldownCoroutine = coroutine.create(function()
    while task.wait(1) do
        if not (cooldown == 0) then
            cooldown -= 1
        end
    end
end)

local function IsCooldownActive(): boolean
    return cooldown > 0
end

local function ResetCooldown()
    cooldown = 0
end

local function StartCooldown()
    cooldown = cooldownTime
    coroutine.resume(cooldownCoroutine)
end

local function PauseCooldown(): boolean
    if not cooldownCoroutine.isyieldable() then return false end
    coroutine.yield(cooldownCoroutine)
    return true
end

local function UnpauseCooldown()
    coroutine.resume(cooldownCoroutine)
end

ResetCooldown()
UnpauseCooldown()
3 Likes

corountines maybe?, you can start them as other thread with task.wait() and then stop it when you wan’t, you can make module too

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.