This tutorial is my attempt to document Roblox’s many time-related features. This is in no way comprehensive or even well-polished, so suggestions and feedback are more than welcome. If you have relevant examples, caveats, notes or really anything: please do reply!
Small disclaimer: This is intended to be a surface-level guide. As always, there are exceptions and details that I simply can’t cover here. Partially because they are not well-documented anywhere, and partially because I am not knowledgeable enough to do so anyway. Things like leap seconds creating UNIX timestamps that point to two points in time, leap smearing affecting microsecond accuracy, monotonicity, etc, are not covered. But if you have information on how Roblox handles these, please do reply
Accuracy is the documented figure to which you can assume a given timestamp is true relative to what it attempts to measure. E.g., if a function is documented as measuring the number of seconds since the UNIX epoch, you can assume it is accurate to 1 second.
Precision is the given number of digits. This can be inconsistent. Decimals past something’s given accuracy should be assumed to be inaccurate/a floating-point error.
Constant is whether this figure is updated in a frame. If a function is constant, then calling it in the same frame will return the same result. This was tested by calling the same function at RunService.PreRender
and then again at RunService.PostSimulation
, which should be at two ends of the Task Scheduler frame pipeline.
Universal is whether this figure should be universal across clients or the server. This does not necessarily mean they are synced. E.g., os.time()
should return the number of seconds since the UNIX epoch. This should be a universal value, but since it relies on each client’s own set time, it can be desynced from other clients and the server.
Getting time
os.time()
- Returns the number of seconds since the UNIX epoch.
- Accuracy: 1 second
- Precision: 0 digits; 1 second
- Constant: No
- Universal: Yes
os.clock()
- Returns the number of seconds of CPU time used by a program.
- Accuracy: <1 microsecond
- Precision: 7 digits; 1/10th of a microsecond
- Constant: No
- Universal: No
tick() - Deprecated
- Returns the number of seconds since the UNIX epoch, on the computer the code was ran on
- Accuracy: ~1 microsecond
- Precision: 7 digits; 1/10th of a microsecond
- Constant: No
- Universal: No
- See this post for deprecation info,
tick()
has large discrepancies and an alternative should almost always be used
time()
- Returns the time since the game began on the computer the code was ran on.
- Accuracy: 1/240th of a second
- Precision: 17 digits; 1/100th of a femtosecond
- Constant: Yes
- Universal: No
elapsedTime() - Deprecated
- Returns the time since Roblox started running.
- Accuracy: 1 second
- Precision: 13 digits; 1/10th of a picosecond
- Constant: No
- Universal: No
- Inconsistent behavior across OSes, can be extremely outdated on mobile
workspace:GetServerTimeNow()
- Returns the number of seconds since the server began.
- Accuracy: 1 microsecond
- Precision: 6 digits; 1 microsecond
- Constant: No
- Universal: Yes
workspace.DistributedGameTime
- Returns the time since the game has been running on the computer the code was ran on.
- Accuracy: 1/60th of a second
- Precision: 16 digits; 1/10th of a femtosecond
- Constant: Yes
- Universal: No
DateTime.now().UnixTimestamp
- Returns the number of seconds since the UNIX epoch. Identical to
os.time()
? - Accuracy: 1 second
- Precision: 0 digits; 1 second
- Constant: No
- Universal: Yes
DateTime.now().UnixTimestampMillis
- Returns the number of milliseconds since the UNIX epoch
- Accuracy: 1 millisecond
- Precision: equivalent to 3 digits; 1 millisecond
- Constant: No
- Universal: Yes
RunService time
and deltaTime
event parameters
-
RunService
has multiple events tied to various stages in the Task Scheduler pipeline. Each one hastime
and/ordeltaTime
parameters that measure the time since the last event in the previous frame, and/or the time to the event in the next frame. See Quenty’s reply in this thread for more details. - Accuracy: Variable, from 1/60th to 1/241th of a second.
- Precision: 17 digits; 1/100th of a femtosecond
Using time
os.difftime(t2, t1)
- Returns the difference between two times,
t2
andt1
must be integers representing seconds since the UNIX epoch - Accuracy: 1 second
- Precision: 0 digits; 1 second
- Constant: Yes
- Universal: Yes
DateTime
DateTime
is as a useful data type that allows you to store timestamps, accurate and precise to the millisecond, in DateTime
objects. From these, you can convert them to local (timezone-specific) times and dates, convert them back, and even get human-readable strings from an otherwise arbitrary timestamp.
Examples
Benchmarking: Store the initial time of os.clock()
in a variable, run a loop, then print the difference between the current os.clock()
and the stored os.clock()
to get a high-accuracy benchmark of the time it takes for your loop to finish.
Getting server uptime: Call workspace:GetServerTimeNow()
and truncate its decimals to get the number of seconds the server has been running, on either the client or server.
Measuring frame rate: Divide 1 by the RunService.PostSimulation()
parameter, deltaTimeSim
to get how often the client or server updates its physics simulation. I.e., its frame rate. Note that the physics simulation may pause under heavy load, or if explicitly paused. Use RunService.PreRender()
's deltaTimeRender
if you would like to account for this, however running code in the render step may negatively affect your players performance.
Converting a date and time for each player’s timezone: Convert your date to a DateTime
object through one of its various constructors. Pass this object to the client, and have the client use DateTime:FormatLocalTime()
to get a string you can use to display a given date and time in a player’s local timezone.
Thanks
-
@Quenty’s excellent reply: Luau Recap: June 2020 - #14 by Quenty
-
zeuxcg’s clarifications on Quenty’s reply: Luau Recap: June 2020 - #16 by zeuxcg
-
@Planet_Dad’s post: Os.time() vs. tick() vs. os.clock() vs.?
-
@Ethanthegrand14’s post: Is there any real difference between os.time() and tick()?
-
@roby336’s post: Simple to learn notes on time() / tick(), For animation or movement scripts and so forth
-
@round_edsquare for pointing out info on
tick()
-
Roblox’s many documentation pages