How to get network latency for server compensation logic?

I’ve been tackling a problem I remembered noticing when in a fight with a playtester where melee originally behaved perfectly when hitting a stationary rig in studio but becomes nearly impossible when attacking a moving target.

I pretty quickly realised this was most likely caused by network latency and worsened since I have hit validation on both server and client. (If you’re interested here was my thought process)

Even when passing hit validation the client, it often fails for a moving target because by the time you request the server to doublecheck that hit, the other player has typically already moved away and the server denies the request. And obviously, hitting early to compensate for this network latency will just mean you fail the range check on the client and the request is never sent in the first place.


So far I’ve successfully guess-worked new logic on the server to compensate for network latency, by creating a new variable being the enemies’ velocity, multiplied by a constant representing network latency. This can then be added onto the player’s position throughout my hit validation logic.

Pseudocode because my explanation was probably horrible

--before;
local PlayerPosition = Character.Position

--now;
local Compensation = Enemy.Velocity * 1/4 -- note this "1/4" constant!
local PlayerPosition = Character.Position + Compensation

After playtesting with a moving target, even in studio this change makes a significant improvement in false negatives, however you may have noticed my issue; although 1/4 was the best constant I found after playtesting a few times, I don’t have, or know how to find, the real value for “network latency”. Therefore, the occasional false negatives begin occurring reliably again once I alter studio’s “Incoming Replication Lag” setting and exaggerate/change the network latency, making the constant no longer applicable.

I’d like to reiterate, this method is working for me, I’m just missing a proper way of finding network latency for my “Compensation” variable.


I frankly have no idea how to calculate the correct value; my only original guess was to use the Player’s “GetNetworkPing” method, but I quickly realised that in studio your normal ping is obviously 0, so this cant what I’m looking for when we’re assuming the correct value to be near 1/4 :sweat_smile:


EDIT: I’m not sure how to include this into the original post so I’ll just add it onto the post; I decided to go into my hit validation scripts and, right after the client and server scripts both calculated what they percieved the distance to be, printed out so we can see the distances the client and server percieve after the compensation update, in this case i found 335/1000 to give a difference of distance of around ~0.15 studs even at high speeds which I’m more than happy with;

what I’m confused on though is the correlation between the number 335/1000 and the actual latency between the client & servers’ print? it looks as though the overall time between the client calculating the distance and then the server is only about 10-15ms on average, which is waaay smaller than the supposed 335ms? again, im truly confused why this constant is working and how to actually calculate it inside the program instead of a manually-written constant :confused:

image

1 Like

You can use Player:GetNetworkPing() to get the current ping. I believe that number is the time the last server packet took to arrive.

You probably want to add some smoothing onto that number so that if one packet is slow it doesn’t result in crazy ping guesses.

For your equation, the exact math would be:

local ping = -- Players.LocalPlayer:GetNetworkPing() but with smoothing
local Compensation = Enemy.Velocity * ping
local PlayerPosition = Character.Position + Compensation

You can also factor in acceleration (for things effected by gravity, for example) by adding 0.5 * acceleration * ping * ping to the velocity compensation (this wouldn’t be good for players because they aren’t controlled by physics).

3 Likes

Sadly I’ve already tried using this method and explained why it failed to work for me;

In studio, ping is virtually 0 and so the “Compensation” variable cancels out and means nothing is actually done.

I have thought about it for a while though and this definitely beginning to confuse me; if there is supposedly virtually 0 ping between the client and server in studio then why is 1/4 a close guess for the actual value for whatever latency is going on? how do i find where that extra latency is coming from??

2 Likes

I think studio has a simulated ping value:

File → Studio Settings → Network → Diagnostics → Incoming Replication Lag

If the value defaults to non-zero it’s weird that it doesn’t also affect the GetNetworkPing return value.

It’s also possible that extra lag in the positions is from interpolation (the client smooths the values it gets from the server so it doesn’t look choppy).

1 Like

Again, I already attempted to use this feature when debugging for the original post but yes “Incoming Replication Lag” was set to zero when I tried using GetNetworkPing and got zero, and I would assume it would print the same value as if I were to change the config value however I’ve logged off and I’m heading to sleep now so I can’t check to say for certain.

Not really sure if this is the case since you can see in the edit I made to the original post that I did around 4 melee requests in succession and they all returned consistent distance values and latency between the client/server printing, sorry I forgot to mention I made that edit to the original post after your reply

1 Like