Is there a way to detect what direction the player is moving without two separate MoveDirection variables?

Heya Everyone!!

I’m making a basic movement script that currently only contains a dash functionality currently. I’m using two separate MoveDirection variables to detect if the player is moving forward, backwards, or sideways. I want to make it so I can only use one.

Any suggestions?

--[[SERVICES]]--
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local Debris = game:GetService("Debris")

--[[PLAYER MODEL]]--
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local RootPart = Character.PrimaryPart

--[[VARIABLES]]--
local DASHCD = false
local IS_ON_SHIFTLOCK = false

--[[FUNCTIONALITY]]--
--//Movement functions
local function DASH()
	if DASHCD then return end
	DASHCD = true
	
	local ForwardMoveDir = math.round(Humanoid.MoveDirection:Dot(RootPart.CFrame.LookVector))
	local SidewayMoveDir = math.round(Humanoid.MoveDirection:Dot(RootPart.CFrame.RightVector))

	if ForwardMoveDir == 1 then
		--//TBA
	elseif ForwardMoveDir == -1 then
		--//TBA
	end
	
	task.wait(1)
	DASHCD = false
end

--//Binding
ContextActionService:BindAction("DASH", DASH, false, Enum.KeyCode.LeftShift)

So, Use one value: Humanoid.MoveDirection then convert to local space then classify, so no need for extra stuff, here:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum  = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")

local DASH_CD = false

local function dash()
	if DASH_CD then return end
	local worldDir = hum.MoveDirection
	if worldDir.Magnitude < 0.01 then return end -- no input

	DASH_CD = true

	-- ONE direction value, made local to the character
	local localDir = root.CFrame:VectorToObjectSpace(worldDir)
	local f = localDir.Z   -- forward is negative Z in Roblox
	local r = localDir.X   -- right is +X

	-- choose the primary direction with a threshold
	local TH = 0.5
	local dir
	if f <= -TH then dir = "Forward"
	elseif f >=  TH then dir = "Backward"
	elseif r >=  TH then dir = "Right"
	elseif r <= -TH then dir = "Left"
	else dir = "Diagonal" end

	-- do your dash per dir
	-- (example impulse)
	local dashSpeed = 60
	local dashVec = Vector3.new(r, 0, f).Unit * dashSpeed
	root.AssemblyLinearVelocity = Vector3.new(dashVec.X, root.AssemblyLinearVelocity.Y, dashVec.Z)

	task.delay(0.9, function() DASH_CD = false end)
end

game.ContextActionService:BindAction("DASH", function(_, state)
	if state == Enum.UserInputState.Begin then dash() end
end, false, Enum.KeyCode.LeftShift)

Paste this anew and it should work

1 Like

Couldn’t you just cross and dot? without making variables?

PlayerModule (one that assigned to Player.PlayerScripts on startup) have method that return normalized vector2 that represents local move direction

What does both task.delay(0.9, function() DASH_CD = false end) and game.ContextActionService:BindAction("DASH", function(_, state) if state == Enum.UserInputState.Begin then dash() end end, false, Enum.KeyCode.LeftShift) even do?

(This looks promising dw)

task.delay(0.9, function() 
    DASH_CD = false 
end)

Just delays given function to be executed 0.9 seconds later when it’s read

game.ContextActionService:BindAction("DASH", function(_, state) 	
    if state == Enum.UserInputState.Begin then 
        dash() 
    end 
end, 
false, 
Enum.KeyCode.LeftShift)

This binds action named “DASH” to key LeftShift with given function. The boolean (false) is an parameter for the BindAction, which just tells the service, if it should create touch button for mobile players. Tho you can reference streight to ´ContextActionService´ variable you defined at the top of your original script instead of the game.ContextActionService.

Here are the links to documentation:
https://create.roblox.com/docs/reference/engine/libraries/task#delay
https://create.roblox.com/docs/reference/engine/classes/ContextActionService#BindAction

Thank you very much for the information.

Anyway, it seems to work but there is a bug with it. If I look anywhere aside from looking in the front direction (-X if I recall correctly), the dash will ONLY dash forward.

External Media
1 Like