Time
Time provides the current simulation time, the current tick, and conversions between seconds and ticks.
Purpose and processing
Section titled “Purpose and processing”Time.Seconds and Time.Tick read the current simulation clock of the context running the module. They do not start a timer or invoke a callback. Use Scheduler to run code later.
SecondsToTick() and TickToSeconds() are arithmetic conversions based on network tick duration. They do not wait for the given time or change match progression.
Key points
Section titled “Key points”SecondsandTickare read-only;- in the current version, one tick lasts approximately
1/60of a second; SecondsToTick()is sensitive to floating-point precision;- converting ticks to seconds and back does not guarantee the original value;
- the API is available in both client-side and server-side Lua.
Current time
Section titled “Current time”| Property | Type | Access | Description |
|---|---|---|---|
Seconds |
number |
get |
Current simulation time in seconds. |
Tick |
integer |
get |
Current simulation tick. |
local currentSeconds = Time.Secondslocal currentTick = Time.TickAssigning either property raises a Lua access error:
Time.Seconds = 0 -- Lua access errorTime.Tick = 0 -- Lua access errorDuring a measured 0.5 seconds, Tick advanced by 30, confirming that both properties follow the same simulation timeline.
SecondsToTick
Section titled “SecondsToTick”Time:SecondsToTick(seconds: number): integerConverts a duration in seconds to a number of ticks.
local ticks = Time:SecondsToTick(0.1) -- 6The method accepts zero and negative values.
| Seconds | Result |
|---|---|
0 |
0 |
0.1 |
6 |
0.25 |
14 |
0.5 |
29 |
1 |
59 |
2 |
119 |
-1 |
-59 |
Ensuring a minimum duration
Section titled “Ensuring a minimum duration”For a positive duration that must not finish one tick early, round the ratio up:
local function SecondsToTicksAtLeast(seconds) local tickDuration = Time:TickToSeconds(1) return math.ceil(seconds / tickDuration)end
local halfSecond = SecondsToTicksAtLeast(0.5) -- 30local oneSecond = SecondsToTicksAtLeast(1) -- 60This approach was verified with durations of 0.1, 0.25, 0.5, 1, 1.5, and 2 seconds.
TickToSeconds
Section titled “TickToSeconds”Time:TickToSeconds(ticks: integer): numberConverts a number of ticks to seconds.
| Ticks | Result |
|---|---|
0 |
0 |
1 |
0.016666667535901 |
6 |
0.1000000089407 |
30 |
0.5 |
60 |
1.0 |
600 |
10.000000953674 |
Small fractional tails are normal floating-point representation errors.
Round trips
Section titled “Round trips”Do not assume that converting twice restores the source value:
local seconds = Time:TickToSeconds(60) -- 1.0local ticks = Time:SecondsToTick(seconds) -- 59, not 60For tested values of 1, 6, 30, 60, and 600 ticks, the values 30 and 60 lost one tick after a round trip, while the others were restored exactly. Keep values in ticks when the rest of the logic also operates in ticks.
Tick-based timer
Section titled “Tick-based timer”In a Reflex module’s server.lua, store a target tick and compare it inside Scheduler:OnTick():
local durationTicks = math.ceil(0.5 / Time:TickToSeconds(1))local finishTick = Time.Tick + durationTickslocal subscription
subscription = Scheduler:OnTick(function() if Time.Tick >= finishTick then -- At least 0.5 seconds of simulation have passed subscription:Cancel() endend)When a callback only needs to run once after a delay, Scheduler:Schedule() is usually simpler.