Ad
Log in to vote
0

Can someone explain os.time() to me? I'm so confuesed!

Asked by
MattVSNNL 116
5 days ago

I've been seeing a Roblox game making a text that shows the real-time and week, And I dug a bit and figured out it's called os.time(), I looked on youtube a bit and found no videos that teach you how it works, So the only logical thing I figured to do was ask someone, So can anyone tell me how it works and maybe put an example about it? Thank you!

0
Its just basically a certain number of seconds from a time. You just convert it to minutes by dividing by 60. SImple MATH BMWLux 0 — 4d

2 answers

Log in to vote
0
Answered by 4 days ago

os.time returns the number of seconds that have passed since 1st January 1970, also known in Computing as the UNIX Epoch.

os.time, rather than os.date is useful for making timers or stopwatches. For example, if I wanted to time how long it takes a player to press a button (in seconds) I could do something like this:

local Button = game.Workspace.Button

game.Players.PlayerAdded:Connect(function()
    local start = os.time()
end)

Button.MouseClick:Connect(function()
    local end = os.time()
    local diff = end - start
    print("You took "..diff.." seconds to press the button since you joined!")
end)

For the sake of brevity, we'll assume this is a single-player game. For multiplayer we'd usually store times individually for every player who joined.

Ad
Log in to vote
0
Answered by 4 days ago
Edited 4 days ago

hey, take a look at this devhub post here
https://developer.roblox.com/en-us/api-reference/lua-docs/os
os.time() actually gives the number of seconds since a particular chosen date called the unix epoch which is pretty standard among many languages. you can translate this into a date yourself, but that's a bit confusing. luckily, you can use the os.date() feature and pass in os.time() for the current time, a process which is also described in that article :)

Answer this question