Client time running ahead of Server time

The Issue: The client seems to be running at a time ahead of the server, is this a client sided issue?

Brief Overview: I have an ability system where players press a key and launch a projectile. The projectile aesthetic is shown on the client while the actual calculations take place on the server. In order to account for ping, I send a “startTime” to the server which is the client’s tick() value. I then take the tick() value on the server and subtract them.

local serverTick = customTick()
local timeDifference = serverTick - startTime

I understand that the tick() value is localized to the client’s timezone, so I adjusted for this with a custom function that adjusts for the offset using a method that looks like this…

local TIME_OFFSET = math.floor((os.time() - tick()) / 100 + 0.5) * 100
-- Rounded to the nearest 100th

function SharedFunctions:GetTime()
	return tick() + TIME_OFFSET
end

From all my testing, this method works perfectly as os.time() is the same on both the server and client (though doesn’t account for units smaller than seconds). I am in the EST time zone, so the offset ends up being 18000.

Main Problem:
When I run tests on the server to see the time difference, I get negative values. Theoretically this shouldn’t be possible. If you gather a time value, send it to the server with some delay included, and get another time value, it should be guaranteed that the 2nd time value be larger than the first. However this is not the case. Often in my testing the client time value is larger than the server time value from anywhere between (0.01 - 0.5) seconds

image

Is this a known issue that has a solution, or is it something unavoidable that cannot be fixed? As you can imagine, when factoring in a negative timeDifference, the side effects from the ping get amplified instead of fixed.

I wouldn’t use tick(), os.time(), etc for getting synchronized times from the server and client. GetServerTimeNow was made specifically for this purpose and is microsecond precise.

5 Likes

Oh wow, thanks! I never knew this existed - I will try it out. When was this added?

It is still pending, but hopefully that works for you

1 Like