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
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