Making a round timer without using wait?

I’ve been thinking about a method on making a round timer without using wait. However, I have absolutely zero idea on how to avoid using wait. So how would I do that? If you don’t know why I want to do this check this resource out:

1 Like

But the article is about wait() not wait(n), n > 0 then why do you want to avoid it? Also: it’s impossible to make a round timer without wait in a sensible way.

Actually you can use RenderStepped and tick() to check if a certain amount of time has passed but what is the point of that if you can simply use wait.

1 Like

To be precise, this article is for beginners who spam “if” or similar requirements with loop, like
repeat wait() until epic == true do

Which is something i actually used to do a long time ago, simply because i was not aware that Changed or OnClientEvent & similar behavior that is triggered upon call existed.

Got a solution, here’s an OOP approach using Quenty’s nevemore engine

-- Timer manager
-- @classmod Timer

local Timer = {}
Timer.ClassName = "Timer"
Timer.__index = Timer

local require = require(game:GetService("ReplicatedStorage"):WaitForChild("Nevermore"))

local Maid = require("Maid")
local Signal = require("Signal")

local RunService = game:GetService("RunService")
local Stepped = RunService.Stepped

function Timer.new(time)
    local self = {}
    setmetatable(self, Timer)

    -- state
    self._time = time
    self._timerTask = nil

    -- lifecycle
    self._maid = Maid.new()
    self._signal = Signal.new()

    return self;
end

function Timer:SetTime(time)
    self._time = time
end

function Timer:GetTime()
    return self._time
end

function Timer:Start(endTime)
    if not self._timer then
        self._timerTask = self._maid:GiveTask(
            coroutine.wrap(function()
                local elapsed = 0
                while elapsed < endTime do
                    elapsed = elapsed + Stepped:Wait()
                    self._signal:Fire()
                end
                self._maid:DoCleaning()
                self._timerTask = nil
            end)
        )

        self._maid[self._timerTask]()
    end
end

function Timer:Connect(newFunction)
    return self._signal:Connect(newFunction)
end

return Timer;
2 Likes