How To Use os.time()?

Hello! :wave:

How can I use os.time because I want to make a shop system that resets after every 60 seconds. So I was wondering how can I use os.time()? Thanks! :wave:

2 Likes

To be honest, I don’t know what os.time() is, but after a single google search, I found some information., you can use tick also. Here is a link to where you might be able to find some help on this topic:

I personally use tick to count my time, here is an example of tick():

local timePassed = tick()

if timePassed - tick() >= 5 then
   print("5 or more seconds have passed!")
end

Sorry if I couldn’t directly help you, I’ll try to find more documentations on this subject!

EDIT: This might help you: How To Make A Daily Reward - Roblox Scripting Tutorial - YouTube

5 Likes

tick() will be deprecated soon, I recommend using os.time()

8 Likes

How come when I print tick() it prints weird numbers and not my time?

1 Like

os.time() prints the number of seconds passed from the Unix Time Epoch (1st January, 1970, midnight) and not the current time / date. For getting current time/date, use os.date(). More details here:

12 Likes

OS currently serves the purpose of providing information about the system time under the UTC format. It has been heavily sandboxed from the standard Lua os library and does not allow you to perform any system-altering operations.

You can find helpful info on:
Developer WIKI: OS

Take a look at this: (Taken from the WIKI)

print(os.time()) --> 1586982482 (ran at approx. April 15th, 2020 at 1:28 PM PST)
print(os.time({
   year=2020, month=4, day=15, -- Date components
   hour=16, min=28, sec=0 -- Time components
})) --> 1586968080
3 Likes

create a variable which is just tick. local something = tick() then print that - tick() like this: print(something - tick()) and you will get the elapsed time from that variable. I recommend using this with functions.

1 Like

If you don’t care about accuracy, you can use a simple wait(60) on a loop. This is the simplest option. If you need accurate, frame-perfect time counting, connect a function to RunService.Stepped and count the time using the delta-time value passed to the connected function.

Another option is Workspace.DistributedGameTime, which counts time since the game started. This is likely the most useful option for gameplay if you’re not counting time yourself with Stepped.

Both of the above options are recommended for gameplay coding. If Roblox ever added the ability to pause or scale time, your game would be ready to go. If you are looking something that is more suited to measuring IRL time, these are your options:

os.time() gives you what is essentially a value that will be about the same no matter the device. This is useful for keeping track of IRL time only; a common use case being daily rewards in your game. You should not use it for timing gameplay stuff, as it does not leave any room for time scaling (can’t be paused, can’t be slowed down or sped up). The others in this thread have linked the docs and described how it works to a tee.

tick() should NEVER be used to count time. It is an antiquated function that was once useful for measuring performance. os.clock() is now the go-to for this use case. tick() has no guarantee of consistency, and anytime you use it is probably better served with Workspace.DistributedGameTime. Again, don’t use it.

9 Likes

Just gonna echo my response to OP above - don’t use tick() like this. Workspace.DistributedGameTime will suit this purpose much more appropriately.

+1 for the AlvinBlox video, it’s good albeit quite long.

2 Likes
local timeOnJoin = nil

game.Players.PlayerAdded:Connect(function(player)

    timeOnJoin = os.time()

end)


game.Players.PlayerRemoving:Connect(function(player)

    print(player.Name .. " has been in the game for " .. os.time() - timeOnJoin .. " seconds")
end)

What does that do? And basically, os.time() just gives you the amount of seconds passed since the unix epoch and you can use os.time() to compare time and is very useful?

If this is in a server script, the value will constantly change, as a player joins. If it is not, the Events won’t even fire.
Thus, say for example, you stay in the game for 1 hour, but leave 2 seconds after I join. It’d still show 2 seconds for you. A better way of approaching this would be to store the join time in a table.

local TimeOnJoin = {}

game.Players.PlayerAdded:Connect(function(Player)
    TimeOnJoin[tostring(Player.UserId)] = os.time()
end)

game.Players.PlayerRemoving:Connect(function(Player)
      print(player.Name .. " has been in the game for " .. os.time() - TimeOnJoin[tostring(Player.UserId)] .. " seconds")
      TimeOnJoin[tostring(Player.UserId)] = nil
end)

This would terminate the logical error. :smiley:

I would only recommend using tick if you are trying to get the exact millisecond. (A way to make generate numbers that will never be the same)

1 Like