Question related to tick() and os.clock() which one should be used here?

Im trying to cook a quick game that has one of those “grows / earns offline” type things and it works just fine using tick() but i noticed that for some reason my Studio seems to be in the future while the actual published game seems to be in history or sum

Like i know it sounds weird but the bug just made it so when i play it studio and hop to play the actual game instead of “earning offline” it will go into debt since well the data saved is telling the script that i left the game IN THE FUTURE? (And no this isn’t a bug with my code i ended up checking by comparing a studio tick() and ingame server tick() at the same time and the studio one was indeed in the future)

I could use os.clock() like i said in the title but i have two problems with that, first being i don’t wanna get rid of the current player data (since if i did decide to use os.clock() i would have to somehow erase lastPlayed for everyone)

In short if you are lazy to read all this junk explaning, is there a way to keep using tick() while not having my actual game believe i was in the future or do i just have to try and move to os.clock() (btw i know i could and should just use Mock data for studio testing but since the game is legit based / depends on offline growing / earning i need it to save so i can check if there are any bugs w it)

I don’t wanna make my own reply a solution but, i ended up just using Mock data and seperate data for studio testing

you can also just use time() , which returns the current timestamp as a unix timestamp.

tick() is not the thing you want to be using for saving data, because tick() is relative to the servers start time, kind of like workspace.DistributedGameTime or whatever it was called. So in your case, I’d say just use time() since it works pretty well.

1 Like

You have it the other way around!

time() (not to be confused with os.time) returns the amount of time, in seconds, that has elapsed since the current game instance started running.
tick() returns how much time has elapsed, in seconds, since the Unix epoch, on the current local session’s computer.

Neither of these would be a good fit for OP, as tick() has several issues as well.

tick() isn’t officially deprecated, but has a variety of issues. It can be off by up to one second and returns inconsistent results across time zones and operating systems. Use os.time(), os.clock(), or time() instead.

I suggest using the DateTime data type instead.

Wait what, I thought time() was the current unix timestamp? tick() is like always starting from 0 or a low value when you check it in studio?

Older APIs don’t exactly have the best naming conventions, but yes, the Roblox global time() does indeed return the amount of time elapsed since the current game instance started.
tick() returns the amount of time elapsed since the Unix epoch according to the device’s time, but has numerous issues and shouldn’t really be used.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.