Os.clock() is correct in studio but is wildly incorrect in the actual client

So i had some concepts in mind, but first i wanted to test something.
This place does:

  1. FPS measurement
  2. ping measurement
  3. how much time it takes to calculate an intensive task
  4. who exactly do those values belong to (and a server one)

Studio:


Expected values. Fake server, fake client, same responsiveness from the same pc.

Client:


How? Why? What?

1 Like

Perhaps it just…. took longer to run.

13682888283ms is still Connection Timeout caliber ping.
And why did it work right in studio?

I didn’t even see that, don’t know why those are your calculated values but I have one idea. For the ping if you are trying to compare os.clock values they are not the same on server and client which is probably why you are getting those crazy values.

EDIT: This is why people try to make time sync modules, but it can be avoided.

Well i appologise for being rude.
But how can i get around it?

Edit: nvm

You can simply calculate the round trip time with remotes.

os.clock measures how long the CPU has been running. The servers in Roblox run for a long time, hence the huge numbers. Using remotes/remote functions would be your go-to solution.


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.

1 Like

Using a time sync module is unnecessary in my opinion.

@VegetationBush You mentioned remotes.
Mine goes something like this:
LocalScript > RemoteEvent fires > Server script sends os.clock() > RemoteEvent fires back > LocalScript calculates

I corrected for that already.

You can do this for round trip:

  1. Store server time
  2. Fire to client via a remote event
  3. Remote client listener fires back to server
  4. Compare new server time with old server time

Of course latency would be included in this time calculation, but you can probably eliminate it with the more results you produce.

I would assume it’s because Studio runs stuff in a Lua VM

We establised it here:

And since i wrote this:

You’re probably sending the clock time from the server to the client, and not calculating it from client only.


With RemoteEvents:

--// Client

local Remote = -- The remote event

local startTime = os.clock()
Remote:FireServer()
Remote.OnClientEvent:Wait() -- Waiting for server response
local ping = os.clock() - startTime

--// Server

local Remote = -- The remote event

Remote.OnServerEvent:Connect(function(Player)
    Remote:FireClient(Player)
end)

With RemoteFuntions

--// Client

local RemoteFunc = -- The remote event

local startTime = os.clock()
RemoteFunc :InvokeServer() -- Waiting for server response
local ping = os.clock() - startTime

--// Server

local RemoteFunc  = -- The remote function event

RemoteFunc.OnServerInvoke = function()
    return
end)

This is only the RoundTrip part, RoundTrip is fine.
I send the server os.clock() back, and since it doesn’t measure game runtime, but hardware time from the actual servers of roblox, it gives huge values. Hence you writing:

EDIT: Also this is round trip ping only:

And i need Client-Server and vice versa. But thank you all, i can work around this since, i know now what causes this.

I’ve seen people use methods like this and just half the value of round trip, this might not be completely accurate but just an idea if you don’t want just round trip.

Also apparently Roblox is adding in a built in way to get the ping of the Player which is probably way more accurate than our hacky methods, but it hasn’t been released yet. Player | Roblox Creator Documentation

image
First time opening it.
But i just need to get the difference between client and server and correct for it.
I’ll be waiting tho.

Yeah that Unsafe message is something to do with the new multithreading stuff, which I also think is not completely rolled out yet.

1 Like

It’s in beta, but there’s a lot of new stuff and a lot being depricated it’s hard to keep up.

happened to me in my game that i used os.clock() on, so i made a number value in replicated storage and it changes from a server script and instead of using os.clock() i used that value and it worked fine