Getting Player Ping

How to get a player's ping using a RemoteFunction

by higio123

Author Avatar

(Please note that this guide assumes that you know what RemoteFunctions are and what they do)

Welcome to my guide! Today I'm going to be showing you a semi-accurate way of getting a player's ping.

First, start off by creating a new local script in StarterPlayerScripts

img|100x30

Then, create a new RemoteFunction in ReplicatedStorage

img|105x25

And afterwards, create a script inside of ServerScriptService

img|105x25

In the local script we're going to need to get the current tick and invoke the RemoteFunction.

To start off, we need to define the RemoteFunction and also the player

local RE = game.ReplicatedStorage.RemoteFunction -- Event
local player = game.Players.LocalPlayer -- Player

After that, we're going to set up a local function called getPing()

local RE = game.ReplicatedStorage.RemoteFunction -- Event
local player = game.Players.LocalPlayer -- Player

local function getPing()

end

We're going to make this function get the starting tick, invoke the server, and then get the ending tick.

local RE = game.ReplicatedStorage.RemoteFunction -- Event
local player = game.Players.LocalPlayer -- Player

local function getPing()
local starttick = tick() -- Get current tick
local servertick = RE:InvokeServer(player) -- Invoke the server and wait till it gets a reponse. This is how the ping is retrieved
local endtick = nil -- Define endtick, but set it to nil
end

Then, we're going to make it wait until it gets a reponse back from the server

local RE = game.ReplicatedStorage.RemoteFunction -- Event
local player = game.Players.LocalPlayer -- Player

local function getPing()
local starttick = tick() -- Get current tick
local servertick = RE:InvokeServer(player) -- Invoke the server and wait till it gets a reponse. This is how the ping is retrieved
local endtick = nil -- Define endtick, but set it to nil
while true do
        wait()
        if servertick == true then -- If it recieves a response then
            endtick = tick() 
            break
        end
    end
    return math.floor((endtick - starttick) * 1000) -- Subtract the time from when it got the response to when it invoked the server
    -- and multiply it by 1000 while using math.floor to make the number whole
end

That's the end of the function! All you have to do now on the local script is call it by using getPing()

local ping = getPing()

Now on the server script, we are just going to make it return true

game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function() -- If it recieves an Invoke on the server then
    return true -- Return true to show that it has recieved the invoke 
end

And that's pretty much it! To see your ping you can do:

while wait(2) do
local ping = getPing()
print(ping)
end

You can also do this the opposite way, by having the script invoke the client. However, invoking the client is strongly discouraged. The client could just disconnect during the invoke and throw an error on both the localscript and the script or leave the server hanging.

View in-game to comment, award, and more!