Trouble with arithmetic and Ping Counter

I’m making a ping counter, and in studio it works like normal. But when I play on roblox, it gives me weird numbers
Here is what I get in studio
image
Here is what I get in roblox
image
Here’s the code
(I don’t want a better way on how to get player ping i just want to find why its happening)

--Server Script
while true do
	wait(1)
	game.ReplicatedStorage.PingEvent:FireAllClients(tick())
end
--TextLabel Script
game.ReplicatedStorage.PingEvent.OnClientEvent:Connect(function(ping)
script.Parent.Text = "Ping: ".. ((tick()-ping)*1000) .. " ms"
print(tick()-ping)
end)

tick() on client and server can be different since client one is based on timezones

3 Likes

That explains a lot thanks. But if thats the case how could i make it so the client and server have the same value for tick()

1 Like

Ping refers to round-trip latency, and this is one of the big reasons. For something as small and precise as ping, the two clocks are never going to be synchronized well enough even if they were in the same timezone.
Use a RemoteFunction instead, fired from the client:

local start = tick()
remoteFunction:InvokeServer()
local ping = tick()-start

Reply from the server:

remoteFunction.OnServerInvoke = function() return true end

Or you can switch the client and server if you wanted the ping to be visible to everyone or readable on the server.

2 Likes

I initially was making a script using functions which looked like this

game.ReplicatedStorage.PingFunction.OnServerInvoke = function(player,ping)
	return ((tick()-ping)*1000)
end

But i was getting the same result, thank you!

2 Likes