The Ultimate os, time, and tick Tutorial!

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.

55 Likes

Any feedback is will help this post!

1 Like

This is a great tutorial, but it would be nice if you could explain how string specifiers work in os.date, since I’ve seen a lot of newer scripters get confused over these. String specifiers are a vital part of os.date, since you can have it return specific parts of the date, for example the current year or current hour. Here’s a helpful chart I found on the DevHub you can put into the tutorial:

5 Likes

@Friendly4Crafter I for sure want to add these. I thought I was going to add them and then I got a bit lazy. lol

Maybe tomorrow I’ll have it done.

Thanks for the feedback!

1 Like

i wonder how to get miliseconds when tick() depracated, so like os.time() do give you seconds but tick does good thing with .39583 you know.

1 Like

@R0mAAn1CH in this case you’d always use os.clock(). I use it instead of os.time() and tick(). All os.clock() is doing is doing the same thing as tick.
Read about it here:

2 Likes

Interesting guide. Could I ask what you want readers to get out of this? Is there a reason why you wrote this?

1 Like

One reason I wrote this was for beginners to learn about these important functions. The other reason was to notify people about tick() getting deprecated.

2 Likes

Even though I feel that your guide is mostly well-written, I also feel the existing documentation hosted on developer.roblox.com suffices; I would disagree that you elaborated much more than what it already provides, even copying it word for word on occasion. On a side note - os.date() is not used once in your code sample for os.date().

4 Likes

Sorry but it didn’t work for me, so I rewrite another script that works. Didn’t anyone notice that it’s not working or it was working back then ?

local day = os.time({year = 2024, 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 years = math.floor(secondsBetween / (60*60*24*30*12))
	local yearRemainder = secondsBetween % (60*60*24*30*12)
	local months = math.floor(yearRemainder / (60*60*24*30))
	local monthRemainder = (yearRemainder % (60*60*24*30))
	local days = math.floor(monthRemainder / (60*60*24))
	local daysRemainder = monthRemainder % (60*60*24)
	local hours = math.floor(daysRemainder / (60*60))
	local hourRemainder = daysRemainder % (60*60)
	local minutes = math.floor(hourRemainder / (60))
	local minuteRemainder = hourRemainder % (60)
	local seconds = minuteRemainder
	
	print(years .. "y:" .. months .. "m:" .. days .. "d:" .. hours .. "h:" .. minutes .. "m:" .. seconds .. "s")
       --1y:6m:5d:14h:39m:31s
	if secondsBetween <= 0 then break end
end

i still dont get it, can you explain how i can get the time that elapsed since an action?

local start = tick()
task.wait(5)
print(tick() - start) --> around 5

Can you set a value using tick()? like setting tick to 0 then it goes and goes and i mean like set the sound volume to tick()

sound.volume = tick()

or how would i do that?

and how would i reverse it?

tick() is good for timers or?