Os.clock for accurate times?

This has probably been asked before but, I can’t find it anywhere so I’m just gonna ask. Can you use os.clock on the server for accurate times. I’m making a stopwatch system, and to do so I’m using os.clock on the server(to prevent exploits). Will the results always be the same? Or is there some case where it may be different.

os.clock is very accurate, since it’s meant for benchmarking.

1 Like

Does that mean If I use it even on the client it would be the same for everyone?

Nope, it’s based on CPU time, so if you run it on the client and server at roughly the same time, there will be very different. You would probably want to use tick, since I’m not sure where you can get decimal time.

I was just asking since I was curious but now I’ve confused myself, if I only use it on the server then for any player that plays will it be the same?

to sum it up, tick() uses a universal clock and Os.clock() uses a personal clock. Os.clock() is more accurate for one person but is worse between multiple. While tick() will be very similar between multiple people while being less precise.
The imprecisions between different people (or in this case, server to client) with Os.clock() will make the resulting number you get different than the one the other may have gotten.

1 Like

What’s your use case, what are you using the stopwatch specifically for? The resolution of os.clock is 1 microsecond but for a timer you’re likely not going to need to know the exact value of the whole part. If the server is doing the counting, use client-side prediction to count the timer down.

Client-side prediction calls for the server to define a point in time and send it to the client. The server and client will then both count this time down on their own environments – the client’s countdown is purely for visual purposes while the server’s represents the real time.

1 Like

I’m using it as a time system for levels, the player can time themselves on how fast they can beat a level, the player doesn’t control the beginning and end of the stopwatch. I only need it for visual purposes, since once the stopwatch ends I’ll send the actual time from the server to display at the end. Are you saying I should try to re create the timer in the client at the same time? My original plan was to modify some int values through the timer in the server, and then have the client display based off those values.

Correct, that’s what I’m saying.

When the stopwatch begins, the server can mark what os.time is and then fire a remote to the client to tell it to start counting as well. The client can then get the current os.clock to serve as a starting point and keep counting until the server tells the client to stop again.

When the stopwatch ends, the client will stop counting the timer. The server will observe the amount of time elapsed as timeEnded - timeStarted and, if you care about server authoritative accuracy, send that via a remote to the client. The client can then correct the time in the display to how much time elapsed as counted by the server.

You will not want to go with your original IntValue idea because you’re asking for the network serving your server to constantly replicate all intermediate values. The server is incapable of updating as fast as clients (a client’s machine has better specs than a server) so while you would get an accurate timer as observed by the server, it’s poor in other areas such as the underlying technical implications. That’s why you use client-side prediction and cut out replication from this system entirely except when you explicitly tell it to replicate using remotes.

Above all though, if this is purely for visual purposes and you aren’t storing any information then I really don’t think you need to use the server to begin with. Ultimately clients are still going to be the one displaying that information unless you show it to other users and they can easily modify values on their own end to misrepresent timing, so you might as well not bottleneck your system with the server for a false sense of security over something that doesn’t need security.

1 Like

I’m planning to make a leaderboard out of these times so it’s necessary people can’t modify their times. Also thanks, I will switch to that, it actually seems easier anyways too. Also you said the server can mark what is.time is, I don’t fully understand, should I use os.time in the server? Also I didn’t see @helghast_1 reply, does this mean even on the server os.clock will be different for different people.

If you need to get the current time on the client so that it will be in sync with the time on the server then call this function


Here is API documentation for it


Will that work if I have my own timer? Also does anyone know for sure if os.clock on the server will be different for different people depending on when they play or for any reason?

I understand GetServerTimeNow() and how I can use it here however I’m still unsure about if os.clock will result in different outputs based on the server or when someone is playing.

os.clock will return different values on the client and the server. If you want a synced timer without sending data back and forth to the server you should use GetServerTimeNow instead of os.clock or os.time

I understand that but I mean disregarding the client if I only ran it on the server then under no circumstances the value would not always be the same if someone started and ended at the same time?