Timer

A timer you started, kept in the tree so you can see it and stop it.

TimeService:After(seconds, callback) and TimeService:Every(seconds, callback) give one, a child of TimeService named like After 0.2. Destroy() stops it, and TimeService destroys an After timer once it has fired. A live timer keeps the run going. An error in its callback is reported at its line, and the program goes on.

TimeService:After(seconds, fn) or TimeService:Every(seconds, fn) gives one.

Example

Repeat, then stopShotScript
-- Every gives a Timer that repeats until it is destroyed; After gives one that fires once.
use TimeService
local ticks = 0
local every : Timer
every = TimeService:Every(0.1, function()
	ticks += 1
	if ticks == 3 then
		every:Destroy()
	end
end)
print("Every: Seconds " .. every.Seconds .. ", Repeats " .. tostring(every.Repeats))

local once = TimeService:After(0.2, function()
	print("never printed")
end)
print("After: Repeats " .. tostring(once.Repeats) .. ", a child of TimeService " .. tostring(once.Parent == TimeService))
once:Destroy()
while ticks < 3 do
	TimeService:WaitAsync(0.1)
end
TimeService:WaitAsync(0.3)
print("ticks, 0.3 s after the third destroyed it: " .. ticks)
What it printedOutput
Every: Seconds 0.1, Repeats true
After: Repeats false, a child of TimeService true
ticks, 0.3 s after the third destroyed it: 3

Properties

NameTypeDefaultDescription
Repeatsbooleanfalse
read-only
True for a timer from Every, false for one from After.
Secondsnumber0
read-only
The delay of an After timer, or the period of an Every timer, in seconds.
From ShotInstance 33