How do I detect if a player is moving forwards or backwards

Currently I have this small code that I have made

    print(hrp.CFrame.LookVector)
	print(h.MoveDirection)
	
	local push = Instance.new("BodyVelocity")
	push.MaxForce = Vector3.new(1,1,1) * 1000000000
	
	local hx =  hrp.CFrame.LookVector.X
	local movex = h.MoveDirection.X
	
	if movex == 0 then
		push.Velocity = hrp.CFrame.LookVector * 35
	elseif hx > 0 and movex < 0 or hx < 0 and movex > 0 then
		push.Velocity = hrp.CFrame.LookVector * -35
	else
		push.Velocity = hrp.CFrame.LookVector * 35
	end
	
	push.Parent = hrp
	Debris:AddItem(push, 0.49)

Its purpose is to move a player forwards when run if they are moving forwards (or if they are standing still) and if they are moving backwards, move them backwards. However I have noticed this being inaccurate (players strafing slightly to the side while moving forwards makes them move backwards e.g)

Any ideas on how I can improve?
(h is referring to a humanoid and hrp is referring to a humanoid root part of a character)

https://www.roblox.com/library/6820777966/Player-move-direction-detector-wasd?ViewInBrowser=true&CreatorId=557883847&SearchId=C4687FB7-9F28-4DAE-B421-A870252D8D5A&Category=Model&Position=1&SearchKeyword=&CreatorType=User&SortType=Relevance
This thing can give you movement of player by wasd and by this you can understand of player walking forward or backwards by W and S, this script works better when player is in first person.
Or you need script which works not only in first person?

Or you need script which works not only in first person?

My game is currently only third person.

ok i just understanded, roblox dont change vector that fast so its a roblox bug i think and script pretty much works if player dont rotating with so big speed, you can test it yourself

userinputservice, if its a server script consider using remoteevents (i dont know if this works im not very good at scripting)

Here is something I came up with, this should be a local script in StarterCharacterScripts. I prefer to use new body movers, but you might want to stick to BodyVelocity if you want. You do not need an attachment if that is the case. I wouldn’t relay on HumanoidRootPart for direction detection, due to strafing and “shift lock” players.

local hrp = script.Parent:WaitForChild("HumanoidRootPart")
local h = script.Parent:WaitForChild("Humanoid")

local att = Instance.new("Attachment")
att.Parent = hrp

local vf = Instance.new("VectorForce")
vf.Attachment0 = att
vf.Parent = hrp

local pushForce = 12500
local standStill = Vector3.new(0,0,-pushForce) 
local rs = game:GetService("RunService")

local connection = rs.Heartbeat:Connect(function()
	
	local x,z = h.MoveDirection.X, h.MoveDirection.Z
	if x == 0 and z == 0 then
		vf.Force = standStill
	else
		vf.Force = Vector3.new(-z*pushForce,0,-x*pushForce)
	end
	
end)

h.Died:Connect(function()
	connection:Disconnect()
end)