How would I measure a player's connection?

So, I did what you told me and still it prints 999ms. Any suggestions?

Put a remote event in replicated storage

Client:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

local function ping()
	local send = tick()
	local ping = nil
	
	remoteEvent:FireServer()
	
	local receive; receive = remoteEvent.OnClientEvent:Connect(function()
		ping = tick() - send 
	end)
	
	wait(1)
	receive:Disconnect()
	
	return ping and math.floor(ping) or 999
end

while true do
	print(ping().."ms")
	wait(5)
end

Server:

local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(player)
	remoteEvent:FireClient(player)
end)

Let me know if you’re still seeing an issue with this setup. The reason you are getting 999 is because ping is still nil when the return is made. Add more time to the wait above the disconnect if the issue persists, possible that the connection is taking long than a second.

image

3 Likes

Only call :Disconnect() when the event is no longer needed.
In this case, you’d want to loop through it as the ping will change overtime.

1 Like

How many ping is one 1 ms? (30 chars)

That should answer it.

1 Like

But the program prints 0ms, although my wi fi sucks… It’s 5Mbps, so I cant have 0 ping.Screenshot 2020-07-28 at 1.21.26 PM

You’re likely testing in studio where your computer is both the client and the server. If you publish your game and go into a live server and access the console you’ll see a different number depending on latency.

1 Like

Thanks! How would I access the console in game?

Press F9, I think you can type /console into the chat aswell.

2 Likes

Great! Thanks everyone for the help! I appreciate it!

I believe it is also possible to simulate it in studio. Under network settings there is a property called IncommingReplicationLag which does what its name suggests.

2 Likes

The same happens in game… Sorry for the delay @EnkryptionKey

That’s because the number being floored is something like 0.034592985 which will constantly output 0. Ping measurements are in the thousandths place of a second, hence the millisecond(ms). To fix this we need to modify the loop.

Changing it from its original form:

while true do
	print(ping().."ms")
	wait(5)
end

Into this:

while true do
	local ping = ping()
	-- Ping was received by the remote within 1 second
	if ping < 999 then
		ping = tonumber(string.format('%.3f', ping*1000))
	end
	print(math.floor(ping).." ms")
	wait(5)
end

string.format will allow us to take the first three decimals of the number, and then we can multiply by 1000 to give us a nicer visual representation of where our ping is at. This solution to getting a player’s ping isn’t exact by any means but it should give you a close indication.

image

2 Likes

Still, the console shows this:

The ping is still 0, although I did what you told me…
But I spotted a Avg.Ping: thing in the top middle of the console. Can’t I just get the calculations from there?

image

My ping is showing accurately in a live server. Just make sure you replaced the while loop and everything is up to date in your code and it should be functional. If you proceed to have issues i’ll send you a place file in pm’s.

3 Likes

I’m having the issue also… I followed all of the steps and its still giving me 0 ping

1 Like

That’s because the Roblox Studio server is hosted on your local machine. Try publishing the game, then playing!

I did. That’s the issue. I can’t get above 0 ping

You can simulate the ping in studio settings:
image

2 Likes

if you are still facing this problem just remove
and math.floor(ping) or 999

1 Like