Struggling to get player moving direction

I’m casually trying to cook up a dashing mechanic for my game. However, I don’t really want to only be able to dash forward. That’s why I tried getting a good player direction detecting script. It’s just that no matter where I get my script or if I make the script myself, the scripts get the direction in which the player is moving according to the whole world. If the player is moving to the east no matter if they’re facing towards the east or not, the scripts will say that the player is moving east.

Idk if anyone gets what I mean, but I simply am just struggling to figure out how to determine if the player is walking left or right like a crab or if they are walking forward or backward like a normal sane person.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local val = script.Parent:WaitForChild("direction")

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if math.round(Humanoid.MoveDirection.X) == -1 then
		val.Value = "Left"
		print(val.Value)
	elseif math.round(Humanoid.MoveDirection.X) == 1 then
		val.Value = "Right"
		print(val.Value)
	elseif math.round(Humanoid.MoveDirection.Z) == -1 then
		val.Value = "Forward"
		print(val.Value)
	elseif math.round(Humanoid.MoveDirection.Z) == 1 then
		val.Value = "Backward"
		print(val.Value)
	end
end)

Just wanna say that I did not make this script, and it came from another forum post from a person I forgot. I do understand the script, tho.

1 Like

You nee to use a dot product. This is a Vector operator, which calculates how close the direction of 2 lines are to each other.

local plr = game.Players.LocalPlayer
local char = plr.Character

local hum = char.Humanoid :: Humanoid
local hrp = char.HumanoidRootPart

hum:GetPropertyChangedSignal('MoveDirection'):Connect(function()

	local dot = hrp.CFrame.LookVector:Dot(hum.MoveDirection)
	print(dot)
end)

If it’s 1 or -1, it’s front to back. If it’s 0, it’s left to right.
012a413ae33634e09a8737c6511744f2(1)

Alternatively just use the MoveDirection for the dash which will allow dashing in any direction the player is walking in (Mobile and Gamepad can move in more directions than keyboard)

for ex:
Root:ApplyImpulse(humanoid.MoveDirection * speed)
(dont use apply impulse for a dash like this though)

I’m kinda confused. How do I use MoveDirection if it’s a read-only thingy? I also don’t get what you mean by not using ApplyImpulse() for “a dash like this.”

Is this a local script or a server script?
If it’s a local script, just use UserInputService to get what keys the player presses and use remote events to convey that information to the server.

local UIS = game:GetService("UserInputService")
local CurrentDirection

local KeyToDirection = {
	[Enum.KeyCode.W] = "Forward",
	[Enum.KeyCode.S] = "Backward",
	[Enum.KeyCode.D] = "Right",
	[Enum.KeyCode.A] = "Left"
}

UIS.InputBegan:Connect(function(inp)
	if KeyToDirection[inp.KeyCode] then
		CurrentDirection = KeyToDirection[inp.KeyCode]
		print(CurrentDirection)
		
		RemoteEvent:Fire(CurrentDirection)
	end
end)

By ApplyImpulse, he was referring to the force of the dash.

It’s a local script. I also don’t want to detect the keys pressed cuz I want the dash to be compatible with other devices.

I successfully made the dash directly use the move direction. I also scrapped the use of a separate script for detecting the moving direction of the player, and having everything inside the main dash script

local me = script.Parent
local debounce = false
local input = game:GetService("UserInputService")
local anim = script.Animation
local player = game.Players.LocalPlayer
local bar = me.Parent.TextLabel
local event = game.ReplicatedStorage.dash
local hum = player.Character:WaitForChild("Humanoid")

local function ultramove()
	if hum:IsA("Humanoid") then
		local movedirection = hum.RootPart.CFrame:VectorToObjectSpace(hum.RootPart.AssemblyLinearVelocity)
		hum.RootPart.AssemblyLinearVelocity = hum.RootPart.CFrame.LookVector * ((movedirection.Z /-1) * 3) + Vector3.new(0, 1, 0)
		hum.RootPart.AssemblyLinearVelocity = hum.RootPart.CFrame.RightVector * (movedirection.X * 3) + Vector3.new(0, 1, 0)
		print("z:" .. hum.MoveDirection.Z)
		print("x:" .. hum.MoveDirection.X)
	end
	
	local track = player.Character.Humanoid:LoadAnimation(anim)
	bar.BackgroundTransparency = bar.BackgroundTransparency + 0.5
	track:Play()
	event:Fire()
end

me.MouseButton1Up:Connect(function()
	if bar.BackgroundTransparency < 0.9 then
		if player.Character.Humanoid.WalkSpeed > 16 then
			if player.Character.Humanoid.Sit == false then
				ultramove()
			end
		end
	end
end)

input.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if bar.BackgroundTransparency < 0.9 then
			if player.Character.Humanoid.WalkSpeed > 16 then
				if player.Character.Humanoid.Sit == false then
					ultramove()
				end

			end
		end
	end
end)

Now I’m struggling with figuring out how to use both the LookVector and RightVector without them overriding each other and ruining the ultra velocity build up dash.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.