Latency for player speed?

Simple Anti-Speed check script I made. In my game there’s no teleportation or other things that would change the players position. My concern is if the players ping is super high. It might cause a false check, is it possible to reduce that false positive or check the ping of the player?

local Players = game:GetService("Players")


local CHECK_INTERVAL = 1 
local MAX_DISTANCE_PER_TICK = 30 

local function monitorPlayer(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local hrp = character:WaitForChild("HumanoidRootPart")

	local lastPosition = hrp.Position

	while player and player.Parent and character.Parent do
		task.wait(CHECK_INTERVAL)

		local currentPosition = hrp.Position
		local distanceMoved = (currentPosition - lastPosition).Magnitude

		if distanceMoved > MAX_DISTANCE_PER_TICK then
			warn(player.Name .. " detected for speed hacking! Moved " .. distanceMoved .. " studs in " .. CHECK_INTERVAL .. "s.")
			character:BreakJoints() -- Kill player
			break
		end

		lastPosition = currentPosition
	end
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		coroutine.wrap(function()
			monitorPlayer(player)
		end)()
	end)
end)
2 Likes

No matter what you do it’s gonna be pretty messy when it comes down to ping. Afaik there isn’t a way of accurately measuring a player’s latency, and even if you can it varies so much it would be hard to compensate. What would probably be best is using a few things such as analyzing how far on average the player moves. This will help rule out really laggy players who have inconsistent movement. You also gotta consider intended teleports (i.e admin commands or smth else in-game) and falling

2 Likes

You can implement Player:GetNetworkPing(), which returns the isolated network latency in seconds, definitely not perfect, but provides some lag compensation.

You can also get the average speed over the last few seconds, which should be able to safely handle lag spikes up to as far back as your data goes.

You may also want to consider reducing the check interval, as sudden direction changes might cause false flags with insufficient leniency (not expensive to check every frame).

2 Likes