Sanity checks for client-sent positions

I always use this function to validate client positions in the server:

local MIN_VERTICAL_SPEED = 15
local MIN_HORIZONTAL_SPEED = 6

local HORIZONTAL_ERROR_MARGIN = 2 -- studs
local VERTICAL_ERROR_MARGIN = 4 -- studs

local CONSIDERED_PING_RANGE = NumberRange.new(0.1, 4)

local function xz(point: Vector3): Vector3
	return Vector3.new(point.X, 0, point.Z)
end

return function(
	player: Player,
	clientPosition: Vector3,
	serverPosition: Vector3,
	assemblyLinearVelocity: Vector3
): boolean
	local relaxedPing = math.clamp(player:GetNetworkPing() * 2, CONSIDERED_PING_RANGE.Min, CONSIDERED_PING_RANGE.Max)
	local positionDelta = clientPosition - serverPosition

	local horizontalThreshold = math.max(xz(assemblyLinearVelocity).Magnitude, MIN_HORIZONTAL_SPEED) * relaxedPing
		+ HORIZONTAL_ERROR_MARGIN
	local verticalThreshold = math.max(math.abs(assemblyLinearVelocity.Y), MIN_VERTICAL_SPEED) * relaxedPing
		+ VERTICAL_ERROR_MARGIN

	local horizontalValid = xz(positionDelta).Magnitude <= horizontalThreshold
	local verticalValid = math.abs(positionDelta.Y) <= verticalThreshold

	--print("-- POSITION VALIDATION --")
	--print("Horizontal valid: " .. tostring(horizontalValid))
	--print("Vertical valid: " .. tostring(verticalValid))
	--print("Position delta: " .. tostring(positionDelta))
	--print("Assembly linear velocity: " .. tostring(assemblyLinearVelocity))

	return (horizontalValid and verticalValid)
end

The problem is when testing in studio, which means there’s not even latency, AND on top of that, a minimum of 100ms is assigned to avoid floating point false positives when in studio, I still sometimes get falsely marked invalid.

What’s wrong in it? Am I being too strict on the calculations? Any overshights of mine?

Help is appreciated.

do not take instantaneous displacements as an indicator of cheating
instead, calculate average velocity of the character inside a time window of given length, say, 8 seconds
and kick only when that is violated
for instance, when character has moved more than 90 meters along a duration of less than 6 seconds

That kinda makes sense but… I’m not using that code as a direct anti-cheat, it’s just to rewind the client-previewed damage in extreme high latency or potentially exploiter individuals.

So you received the signal in the server and gotta validate it at that moment, not after some seconds.

Also, why would it detect an invalid position in Roblox Studio, where latency is 0? That’s my biggest doubt, since it’s considering a minimum latency even if there’s none.

What i did to stop getting that was just increase all the thresholds/error margins a bit. :person_shrugging:

i repeat once again
do NOT try to write multiplayer code which relies on instantaneous time frames or frame-perfect position displacements
it’s very prone to hard-to-find bugs

So how do I deal with that?


Could you elaborate? If the damage is rewinded and everything re-synced (client VFX cleaned, etc.), what bugs could appear?

you’ve asked for advice, i gave it to you
why are you trying to make me disprove my own point?

I’m trying to further understand your point.

I’m asking specifically how to deal with situations where you need an instant check, instant validation, if instantaneous time frames are “very prone to hard-to-find bugs”.

Please be more compassionate.

I think what ivan866_z is trying to say is that you shouldn’t run this code every frame, run it every second and get the average that a player should go in a second.

running per frame/very fast will have weird to find bugs

Also I’m noticing (may be wrong) that you are getting 1 position from client and the server and you are comparing those. A exploiter can just teleport themselves and then send the position where they are teleporting and then bypass this system

What i do is I run a loop every .5 seconds with a variable called ‘LastPos’ (set to the characters position) every loop the server checks lastpos and the current pos of the character (checks if its really far then lags back) and at the end of the loop it sets lastpos to the currentpos/pos the character should be

What I’m trying to explain is this code runs once every signal, a remote event, when I need to validate a client position.


Yes, but the idea is exactly validate this sent position, that’s not necessarily their character’s position; could be an object, etc.


Anyways, please note I’m not trying to create an anti-cheat. Sanity check.

Oh i see, you are making something where the client needs to send their position, but it cant be too far from their actual position

For now you should check to see when it happens
‘When is it happening. { falling,teleporting,ect }’
‘Is it the vertical that usually goes wrong or the horizontal?’

Also think more about if there is a different/better way to go about your original problem that needs this sanity check

If it just seems to happen in randomly then just increase the ping_range.min to be higher
Test this outside of studio too, you may get different answers

It used to happen only when falling (tested in the Roblox player), then I increased the vertical threshold, it was fine like this for a while. I started getting a lot of false positives recently though.

I also can’t understand how this cold EVER invalidate something in studio (it happens for me), isn’t it a 0 latency environment? Like the server and client are in the same machine…

Anyways, I just removed this sanity check, you can ignore this thread I guess.

Falling is tricky as the player always accelerates because of gravity.

for now just increase vertical alot or just dont check for vertical

It really depends on what you are making here, if its simple (e.g a ability) then you can just have it not work if you are falling. For other things you could probably just skip the Y axis/ teleport the player to the correct Y

oops i just realized you removed this sanity check