Dash Direction Problems?

the problem is it does not dash in the correct direction its rather wierd I think it dashes based on the position of the camera If i’m looking left i dash forward and when im looking right I dash backwards



local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")


local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local dashSpeed = 100 
local dashDuration = 0.2 
local dashCooldown = 1 

local dashForwardAnimationId = "http://www.roblox.com/asset/?id=18444537541"
local dashBackwardAnimationId = "http://www.roblox.com/asset/?id=18444567991"
local dashLeftAnimationId = "http://www.roblox.com/asset/?id=18444581032"
local dashRightAnimationId = "http://www.roblox.com/asset/?id=18444586633"
local DodgeAnimationId = "http://www.roblox.com/asset/?id=18444592107"


	local canDash = true

local function setInvincible(state)
	if state then
		humanoid:SetAttribute("Invincible", true)
	else
		humanoid:SetAttribute("Invincible", false)
	end
end

local function playAnimation(animationId)
	local animation = Instance.new("Animation")
	animation.AnimationId = animationId
	local animationTrack = humanoid:LoadAnimation(animation)
	animationTrack:Play()
	return animationTrack
end

local function dash()
	if not canDash then return end
	canDash = false

	local moveDirection = humanoid.MoveDirection

	setInvincible(true)

	if moveDirection.Magnitude == 0 then
		local animationTrack = playAnimation(DodgeAnimationId)
		local startTime = tick()
		local connection

		connection = RunService.Heartbeat:Connect(function()
			local elapsed = tick() - startTime
			if elapsed < dashDuration then
				rootPart.Velocity = Vector3.new(0, 0, 0) 
			else
				rootPart.Velocity = Vector3.new(0, 0, 0)
				connection:Disconnect()
				animationTrack:Stop()
				setInvincible(false)
			end
		end)
	else
		local dashVector
		local animationTrack

		if moveDirection.Z > 0 then
			dashVector = rootPart.CFrame.LookVector * dashSpeed
			animationTrack = playAnimation(dashForwardAnimationId)
		elseif moveDirection.Z < 0 then
			dashVector = -rootPart.CFrame.LookVector * dashSpeed
			animationTrack = playAnimation(dashBackwardAnimationId)
		elseif moveDirection.X > 0 then
			dashVector = rootPart.CFrame.RightVector * dashSpeed
			animationTrack = playAnimation(dashRightAnimationId)
		elseif moveDirection.X < 0 then
			dashVector = -rootPart.CFrame.RightVector * dashSpeed
			animationTrack = playAnimation(dashLeftAnimationId)
		end

		local startTime = tick()
		local connection

		connection = RunService.Heartbeat:Connect(function()
			local elapsed = tick() - startTime
			if elapsed < dashDuration then
				rootPart.Velocity = dashVector
			else
				rootPart.Velocity = Vector3.new(0, 0, 0)
				connection:Disconnect()
				animationTrack:Stop()
				setInvincible(false)
			end
		end)
	end


	wait(dashCooldown)
	canDash = true
end


UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.Q then
		dash()
	end
end)


player.CharacterAdded:Connect(function(char)
	character = char
	humanoid = character:WaitForChild("Humanoid")
	rootPart = character:WaitForChild("HumanoidRootPart")
end)

could you show a video of the issue?

That whole time I was making the same input HoldingW and pressing Q yes For some reason i was getting back dash

These two posts may help you calculate the move direction better