Using ray's to detect walkspeed changes

So I am trying to make an anti-exploit for speed changes, I know Ray’s are the way to do it but I am not the best at using them and I have run into an issue.

It works fine if I run in a straight line but as soon as I turn around it gives me ridiculously large numbers for the distance travelled resulting in a false exploit detection.

Here is the basis of my raycasting:
root is the character’s HumanoidRootPart
start is a CFrame captured a tenth of a second before the function was run

local function castRay(root,start)
	local startPos =,3,start.Z)
	local endPos =,3,root.Position.Z)
	local ray =, endPos)
	local _, pos = workspace:FindPartOnRayWithWhitelist(ray,{root.Parent})
	local distance = (start.p - pos).magnitude
	return distance

This is a screenshot of the output, this is just distance printed after it was returned. When the number goes from around 1 to the mid 80’s that’s when I turned around.

Any help is appreciated.

Can’t you instead use humanoid.Running, since its listeners get the speed the humanoid is running at?

    if speed > max_speed_you_allow + 1 then
        -- something 

I can try that but I don’t think it is replicated.

Oh no it’s not necessarily humanoid.WalkSpeed, like it could be 16.002945882, which i why i put an extra stud

All this time it was so easy. Thank you that helps a lot!