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)
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
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)