PingTimes
by boatbomberI needed ping times to try and negate the impact of network delay in a game I was working on, so I wrote a module that gets it!
Usage:
My goal was to make it as simple and fool-proof as possible to use. Simply require
the module from both Client and Server.
(Note: The client’s require
will return nil, but it’s needed to initialize the client ping responder.)
To get the ping time of a player, you simply index the module with that Player. This can only be done on the Server.
PingTimes[Player]
returns a number that represents network round trip time measured in seconds.
local PingTimes = require(ReplicatedStorage.PingTimes)
local PlayerPing = PingTimes[Player]
The module is behind a proxy table with a metatable that prevents you from breaking the module’s internals, so attempting to manually set PingTimes[Player]
will warn
and discard your change. Attempting to index with something other than a Player Instance will return nil.
Security:
It is important that exploiter can’t fire the remotes of this to change their ping values, so I used the GUID Validation method that was recommended by @berezaa in this thread. The ping must be returned with the correct GUID or it is ignored. Each ping attempt generates a new GUID, so there is no way for the exploiter to know the validation value ahead of time.
To prevent timeout, I make use of the wonderful Postie module by @BenSBk. Major shoutout to that, it made this way easier for me.
Notes:
When a player first joins the game, their ping time might not be accurate yet. Give it a moment so they can establish a stable connection first.
If, for some reason, it cannot get their ping, it defaults to 100 milliseconds since that’s a general average afaik.
It refreshes ping times every 3 seconds, so the data will be reasonably up-to-date without applying undue strain to your network and bandwidth. This is changeable via the UPDATE_FREQUENCY
variable in the module, if you desire.
Test Place:
Module:
Library:
GitHub: