How do you get a player's acceleration?

wait(0.2)
local hrp=game:GetService("Players").LocalPlayer.Character:WaitForChild("HumanoidRootPart")

local pp=hrp.Position
local pv=Vector3.new()
local a=Vector3.new()

game:GetService("RunService").Heartbeat:Connect(function(dt)
	local np=hrp.Position
	local nv=(np-pp)/dt
	pp=np
	
	a=(nv-pv)/dt
	pv=nv
	
	print(a.Magnitude>0 and a.Unit or a)
end)

The acceleration literally reverses direction every fifth frame when you walk straight

While walking straight, your acceleration should be 0.

I don’t think Lua runs often enough (even on Heartbeat) to gauge the physics of objects accurately enough for what you want when using only Position. When walking, the character has a Velocity of 16, but this script routinely reported above and below that number by significant amounts.

If you read the Velocity properly directly then the script seems to work fine:

wait(0.2)
local hrp=game:GetService("Players").LocalPlayer.Character:WaitForChild("HumanoidRootPart")

local pv=Vector3.new()
local a=Vector3.new()

game:GetService("RunService").Heartbeat:Connect(function(dt)
	local nv=hrp.Velocity
	
	a=(nv-pv)/dt
	pv=nv
	
	print(a.Magnitude>0 and a.Unit or a)
end)

That is, it prints 0, 0, 0 while walking, because the character has 0 acceleration when its velocity stays the same.

7 Likes