Timer
Timer utility for scheduling delayed or repeating callbacks. Use for countdown timers, delays, periodic actions, and game events.
Three ways to create timers:
Timer.After(seconds, callback)- One-time delay (most common)Timer.Every(seconds, callback)- Repeating timerTimer.new(seconds, callback, repeats)- Full control
Available on: Client and Server scripts
Properties
True if the timer is currently running, false if stopped. Use to check timer state.
local myTimer = Timer.After(5, function() print("Done!") end)
function self:Update()
if myTimer.enabled then
print("Timer is still running...")
else
print("Timer has finished or was stopped")
end
end
The interval in seconds between timer callbacks. For repeating timers, this is how often the callback fires.
local timer = Timer.Every(2, function()
print("Tick!")
end)
print("Timer interval: " .. timer.interval .. " seconds")
Methods
Creates a one-time timer that fires after the specified delay. Equivalent to Timer.new(interval, callback, false).
This is the most common timer pattern for delays.
-- Delay action by 5 seconds
Timer.After(5, function()
print("5 seconds later...")
end)
--!Type(Server)
-- Welcome message after player joins
function self:ServerStart()
scene.PlayerJoined:Connect(function(scene, player)
Timer.After(2, function()
print("Welcome " .. player.name .. "!")
end)
end)
end
-- Chain multiple delayed actions
Timer.After(1, function()
print("1 second")
Timer.After(1, function()
print("2 seconds")
Timer.After(1, function()
print("3 seconds - done!")
end)
end)
end)
Parameters
interval
callback
Returns
Creates a repeating timer that fires at regular intervals. Equivalent to Timer.new(interval, callback, true).
Timer continues until explicitly stopped with Stop().
-- Print message every 2 seconds
local timer = Timer.Every(2, function()
print("Tick every 2 seconds")
end)
-- Stop it later
Timer.After(10, function()
timer:Stop()
end)
--!Type(Server)
-- Countdown timer
local timeRemaining = 60
local countdownTimer = Timer.Every(1, function()
timeRemaining = timeRemaining - 1
print("Time remaining: " .. timeRemaining)
if timeRemaining <= 0 then
countdownTimer:Stop()
print("Countdown finished!")
end
end)
--!Type(Server)
-- Spawn enemies periodically
local spawnTimer = Timer.Every(10, function()
if scene.playerCount > 0 then
print("Spawning enemy wave!")
-- Spawn logic here
end
end)
-- Pause spawning when no players
scene.PlayerLeft:Connect(function(scene, player)
if scene.playerCount == 0 then
spawnTimer:Stop()
end
end)
-- Resume spawning when players join
scene.PlayerJoined:Connect(function(scene, player)
if scene.playerCount == 1 then
spawnTimer:Start()
end
end)
Parameters
interval
callback
Returns
Starts the timer and resets its elapsed time to zero. For repeating timers, this resets the countdown before the next callback.
local timer = Timer.Every(5, function()
print("Every 5 seconds")
end)
-- Reset timer when something happens
function OnPlayerAction()
timer:Restart() -- Start the 5 second countdown again
print("Timer restarted!")
end
Returns
Starts or resumes the timer without resetting elapsed time. If timer was stopped, it continues from where it left off.
local timer = Timer.Every(5, function()
print("Repeated action")
end)
-- Stop and start timer
timer:Stop()
Timer.After(10, function()
timer:Start() -- Resume timer
end)
Returns
Stops the timer. It will not fire until Start() or Restart() is called. Does not reset the elapsed time (use Restart() for that).
local countdownTimer = Timer.Every(1, function()
print("Tick!")
end)
-- Stop after 10 seconds
Timer.After(10, function()
countdownTimer:Stop()
print("Timer stopped")
end)
Returns
Updated 19 days ago