In this tutorial I’m going to be teaching you guys os
, time
and tick
and what they are and how to use them.
tick()
tick()
returns the number of seconds since January 1st, 1970 according to the devices time. tick()
has now gotten somewhat deprecated by Roblox. It’s still very good but there is other options in the os
library. Read this for more on tick()
getting deprecated : Luau Recap: June 2020 - #6 by zeuxcg
1649938990.301689 -- Number of seconds since January 1st, 1970(when I made the post).
tick()
is also called Unix or EPOCH.
A good way to use tick()
is for coders to control stuff with the time. If you can get it working right tick()
is great for accurate waiting.
A script to see this could be like :
local StartTime = tick()
game:GetService("RunService").Heartbeat:Connect(function()
local CurrentTime = (tick() - StartTime)
if CurrentTime >= 3 then
print("3 seconds up")
end
end)
Keep in mind that this runs on the players device’s time not the global time.
time()
time()
returns the number time in seconds since that game Instance has started running.
This can be good for a message or announcement system or something along those lines. A script for that could be :
game:GetService("RunService").Heartbeat:Connect(function()
if time() >= 5 then
print("5 or more seconds since game start")
end
end)
os
os
is a library filled with 4 different functions. Read more here : os
os.clock()
os.clock()
returns the amount of CPU time used by Lua in seconds. This value has high precision, about 1 microsecond, and is intended for use in benchmarking. Making this a very good substitute for the now somewhat deprecated tick()
function.
An example script for this could be this
-- Record the initial time:
local startTime = os.clock()
-- Do something you want to measure the performance of:
local a, b = 0, 1
for i = 1, 5000000 do
a, b = b, a
end
-- Measure amount of time this took:
local deltaTime = os.clock() - startTime
print("Elapsed time: " .. deltaTime)
--> Elapsed time: 0.044425600033719 (actual number may vary)
Keep in mind that this runs on UTC time so it’s the same for every player.
os.time()
os.time()
returns the number of seconds since the Unix EPOCH, just like tick()
. The o differences is that it runs on the current UTC time, meaning that this is better for Server-sided stuff and that it returns a slightly more floored number than tick()
.
This is essential tick()
for every player at the same time. Good example is the tick()
code and the os.date()
code.
os.date()
os.date()
returns the date in the format of day, month, day of month, hour, minute and year.
Thu Apr 14 12:23:10 2022--When I made this post.
This means os.date()
is great for making a live event or countdown.
A simple countdown script for this could be something like this :
local countdownGui = script.Parent
local countdownText = countdownGui:WaitForChild("CountdownText")
local day = os.time({year = 2020, month = 8, day = 19, hour = 11, min = 16, sec = 0})
while task.wait() do
local secondsBetween = os.difftime(day, os.time()) -- Good example for os.difftime and os.time()
local seconds = secondsBetween % 60
local minutes = math.floor(secondsBetween % (60*60) / 60)
local hours = math.floor(secondsBetween % (60*60*24) / (60*60))
local days = math.floor(secondsBetween % (60*60*24*30) / (60*60*24))
local textString = days .. "d:" .. hours .. "h:" .. minutes .. "m:" .. seconds .. "s"
countdownText.Text = textString
if secondsBetween <= 0 then break end
end
os.difftime()
os. difftime
returns the number of seconds from t1 to t2. The difference is computed assuming that t1 and t2 are correctly casted to the time_t format.
This can be useful for making an in-between variable. An example for this is in the os.date()
section.