Any way to make cooldown without using wait()?

Im currently working on a tower defense game and ive noticed that using wait() for cooldown is a pretty bad option. Its extremely unprecise.

I’ve read somewhere that it adds a few miliseconds and is almost always not accurate.

I was wondering if there were any other ways to make cooldown that dont use wait()? Thanks!

3 Likes

Task.wait is the parallel alternative to wait. Its what we all use now

1 Like

im using task.wait() but for some reason with lower numbers ( like, lets say, 0.05 ) it fires slower than it should ( like if the cooldown was around 0.1 )

my bad, it was something to do with my code, the problem is fixed now!

Here’s three different cooldown systems you can use:

-- Option 1 (debounce)

local debounce = false
Any.Event:Connect(function()
    if debounce then return end
    debounce = true
    -- code here
    task.wait(1)
    debounce = false
end)
-- Option 2 (tick)

local lastinput = false
local cooldown = 1

Any.Event:Connect(function()
    if not lastinput or tick() - lastinput > cooldown then
        lastinput = tick()
        -- code here
    end
end)
-- Option 3 (deltatime)

local halttime = 0

Any.Event:Connect(function()
    if halttime == 0 then
        halttime = 1
        -- code here
    end
end)

game:GetService("RunService").RenderStepped:Connect(function(dt)
    halttime = math.max(0, halttime - dt)
end)
2 Likes

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