Hello scripters! This is my first question soo yeah. My question is.. Is there a way to get the client's ping? I have seen this in games like Arsenal. I tried using this code:
1 | local currentPing = tick() -- epoch time |
2 | while wait( 1 ) do |
3 | currentPing = (currentPing()-tick()) |
4 | print (currentPing) |
5 | end |
but sadly it doesn't work. I even tried using ping meters. Most of them are giving me errors. Any help? Also, I want to make a GUI that can show the player their ping.]
Edit: I used this code:
1 | while wait() do |
2 | local ping = ( 1 /wait()) |
3 | print ( "Your ping is " ..ping) |
4 | end |
and it was fairly better than 0 (no offense).
Please help!
Considering ping is usually round trip time (Client ? Server ? Client), a RemoteFunction would be a good way to measure it. Simply compare the time you invoke the server and the time when it gets back. Something like this:
Client
1 | local function getPing() |
2 | local start = tick() |
3 | RemoteFunction:InvokeServer() |
4 | return tick() - start |
5 | end |
Server
1 | RemoteFunction.OnServerInvoke = function (player) |
2 | return |
3 | end |
You could use a Remote event, and check how long it takes for it to go to the server.
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events
Local script:
1 | local RemoteEvent = -- remote event |
2 | local player = game.Players.LocalPlayer |
3 | while wait( 1 ) do |
4 | local Tick = tick() |
5 | RemoteEvent:FireServer(player, Tick) |
6 | end |
Server:
1 | local RemoteEvent = -- remote event |
2 | RemoteEvent.OnServerEvent:Connect( |
3 | function (Tick) |
4 | local ping = ((tick() - Tick) - 1 ) |
5 | print (player.Name .. " ping is " .. ping .. "." ) |
6 | end |
7 | ) |