So I’m not quite sure how to detect a player moving left/ right/ and backwards. I know Humanoid.MoveDirection is the answer to it but i cant seem to find a good dev forum post that explains it clearly. Replies are appreciated !
5 Likes
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
if math.round(Humanoid.MoveDirection.X) == -1 then
print("Walking left!")
elseif math.round(Humanoid.MoveDirection.X) == 1 then
print("Walking right!")
elseif math.round(Humanoid.MoveDirection.Z) == -1 then
print("Walking forward!")
elseif math.round(Humanoid.MoveDirection.Z) == 1 then
print("Walking backward!")
end
end)
Local script inside StarterCharacterScripts.
32 Likes
Thanks so much. Really appreciate it!
1 Like
Just wanted to add that you’ll also want to convert this to object space, or it’ll be relative to the world and not to the player. So you’d do
movedirection = Char.PrimaryPart.CFrame:VectorToObjectSpace(Humanoid.MoveDirection)
Using Forummer’s code, you’d then just replace Humanoid.MoveDirection with movedirection.
16 Likes
didn’t work for some reason?
i did what you said and it didn’t work…
local MoveDirection = workspace.CurrentCamera.CFrame:VectorToObjectSpace(Humanoid.MoveDirection)
8 Likes