Update GetNetworkPing constantly

Hi, I am using this basic script that puts the ping into an IntValue using the GetNetworkPing function, but it seems to update every 10-15 seconds for some reason?

Script (server):

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local PingFolder = Instance.new("Folder")
	PingFolder.Name = "leaderstats"
	PingFolder.Parent = player
	
	local Ping = Instance.new("IntValue")
	Ping.Name = "Ping"
	Ping.Parent = PingFolder
	
	while task.wait() do
		Ping.Value = math.floor(player:GetNetworkPing() * 2000)
	end
end)

That is probably not a error, It is probably something implemented into Roblox to stop overloading their servers and why do you need to multiply ping by 2000?

the ping is returned in miliseconds so i times by 2000

oh alright

ping is given in seconds not milliseconds

‘IntValue’ objects truncate the decimal from their assigned values already, the use of ‘math.floor’ isn’t necessary. GetNetworkPing returns a value in seconds, so 30 ping (m/s) would translate to 0.03 seconds.

local Game = game
local Players = game:GetService("Players")

local function OnPlayerAdded(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	
	local Ping = Instance.new("NumberValue")
	Ping.Name = "Ping"
	Ping.Parent = Leaderstats
	
	Leaderstats.Parent = Player --Minor optimisation.
	
	while Player.Parent do --Prevents memory leaks.
		Ping.Value = Player:GetNetworkPing()
		task.wait()
	end
end

Players.PlayerAdded:Connect(OnPlayerAdded)

Bare in mind that in studio the server is locally hosted resulting in a ping of 0 (m/s), you’ll need to test in a live session.

1 Like