Help with moveDirection

Disclaimer I have searched, I cannot find anything so far, so if there is something, please point me to it!

So I am trying to use humanoid.MoveDirection(), the only problem is that how I want to use it is to tell if the player is moving forwards, or backwards, or left to right.
So the problem is that MoveDirection returns a vector3, which when I first spawn in and start walking forwards its (0,0,-1) now this would work fine, but if you turn your character to face another direction, it changes, and is no longer (0,0,-1), instead it becomes quite wild, like -0.9332048892974854, 0, -0.3593446910381317, so how can I use moveDirection where it isnt based off of the direction the character is facing,
thanks~
~ Frodev

1 Like

Try using the players move vector directly from the playerModule like this:

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer

local controls = require(localPlayer.PlayerScripts.PlayerModule):GetControls()
local inputVector = controls:GetMoveVector()

print(inputVector) --direction player is moving 
1 Like

Ok this works and all, but it only works for players and via the client. I kinda need to make this work in as many cases as possible

1 Like

Can you elaborate on what you mean by it only works via the client and in as many cases as possible?

pretty simple actually, um yeah I noticed that PlayerScripts isnt on the server . . . so . . .

1 Like

If the server needs the player direction, send remote events. You can’t get player movement direction directly from the server

yeah sending remote events becomes pretty inconvenient when you need to do it every heartbeat, so I kinda need another method.

1 Like

I mean, it’s the most efficient way imo. I guess you could try maybe doing something like getting the difference between the players last position and new position then try and find the world space offset but like that’s pretty insane when the alternative is just sending remote events

yeah the option of comparing positions sadly wont work in my case,

What’s your use case? Maybe there’s an easier way to do it

a lot of cases, basically I want to be able to get the value any time I trigger a function, for instance I could do it just in general, which in that case yeah remove events would work, but I might need to do it every heartbeat, really I need to do this in like any context.

What’s the specific use case though? Like a character controller?

yes probably something like that, or for handling vehicles, something movement related

If that’s the use case, just do the movement directly on the client and let it automatically replicate. It’s way smoother that way

you can use the dot/cross product to determine the players move direction relative to the characters orientation like this:

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

while task.wait() do
	local moveDir = hum.MoveDirection
	local lookDir = hrp.CFrame.LookVector
	local dot = moveDir:Dot(lookDir)
	local cross = moveDir:Cross(lookDir)
	
	if moveDir.Magnitude > 0 then
		if dot > 0.1 then
			print("Moving Forward")
		elseif dot < -0.1 then
			print("Moving Backward")
		end

		if cross.Y > 0.1 then
			print("Moving Right")
		elseif cross.Y < -0.1 then
			print("Moving Left")
		end
	else
		print("Not Moving")
	end
end

this is just an example in a local script loop in StarterCharacterScripts but it should work in other contexts too

If it’s a character controller you would need to get the actual player input to apply the force into the desired direction so this might not work for what they want

True, in the specific case of a character controller you might need to use direct input to perform actions like applying force, im just giving a general solution assuming the default character movement.
Just realised i accidentally replied to you initialy instead of the poster sry

another easier way to find the relative move direction to the player can be like this:

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

while task.wait() do
	print(hrp.CFrame:VectorToObjectSpace(hum.MoveDirection))
end

this will always return
~ 0,0,-1 when walking forward,
~ 0,0,1 when walking backwards,
~ 1,0,0 when walking right,
~ -1,0,0 when walking left,

2 Likes

I am not the OP but I was not aware of this (and I’ve been doing this for like 5 years) so thank you so much, be blessed

1 Like

I mean this works, but the X and Y values are constantly changing, and arent even just 0 even when I’m walking forward so idk about that