MoveDirection.Magnitude returns 0

Hello developers! I have a problem with my dash system, so basiclly I made it that if the moveDirection.Magnitude is bigger than 0 it will let the player dash and it is working perfectly fine, but when the player dies moveDirection.Magnitude will return 0 even if the player is moving(meaning that it can not return 0, still does IDK why).

This is the variable that is being created everytime the player clicks:

UIS.InputBegan:Connect(function(Input, processed)
	if processed then return end
	
	if Input.KeyCode == Enum.KeyCode.Q then
		
		if char:GetAttribute("isRagdoll") == true then return end
		
		local md = hum.MoveDirection.Magnitude
		if dashing or md <= 0 then return end
		
		
		local MoveDirection = camera.CFrame:VectorToObjectSpace(hum.MoveDirection)
		
		local left = math.round(MoveDirection.X) == -1
		local right = math.round(MoveDirection.X) == 1
		local front = math.round(MoveDirection.Z) == -1
		local back = math.round(MoveDirection.Z) == 1

The script is much bigger but I considered this chunk of it useful.
Also the problem doesn’t come from the variable “dashing” because I tested it.

1 Like

I thought it might be because md(magnitude) is being checked/cast into a var before the player actually is moving , therefore returning 0. This is assuming ‘q’ is pressed only once in order to dash. Let me know if I’ve got the wrong idea here though.

1 Like

It might be, let me check. And yes, you need to press just once in order to dash.

1 Like

Nope, the player moves before pressing Q

1 Like

Then I would use print statements to test and see what the magnitude is and when.

1 Like

I already did that, but still the same problem

1 Like

That’s not insightful. Print statements are not going to fix your problem, they’re just put in the code to see when things went wrong. This gives us more of an idea of how to fix the problem. Can you do me a favor and put a print statement in between “local md = hum.MoveDirection.Magnitude” and “if dashing or md <= 0 then return end” and tell me what it prints?

1 Like

Hey @TryingToCoDe2 !

Usually, that kind of problem lies in the way you declare your character variables. If that’s the case, your hum variable might refer to the same humanoid that the character had before it died (thus no longer exists and its MoveDirection is also 0).

Although there are better approaches, but the immediate solution might be:

-- You should have a Player reference for that script
UIS.InputBegan:Connect(function(Input, processed)
	if processed then return end

	if Input.KeyCode == Enum.KeyCode.Q then
		-- Check if the character exists
	    if not Player.Character then return end
	    if not Player.Character:FindFirstChild("Humanoid") then return end

		if Player.Character:GetAttribute("isRagdoll") == true then return end
		
        -- Get the Character directly from the player
		local md = Player.Character.Humanoid.MoveDirection.Magnitude
		if dashing or md <= 0 then return end
		
		local MoveDirection = camera.CFrame:VectorToObjectSpace(hum.MoveDirection)
		
		local left = math.round(MoveDirection.X) == -1
		local right = math.round(MoveDirection.X) == 1
		local front = math.round(MoveDirection.Z) == -1
		local back = math.round(MoveDirection.Z) == 1
1 Like

I know that print statements won’t fix my problem, what I wanted to say is that I printed the MoveDirection or in my case, the md and the print statements are giving me 0.

Wow! I wouldn’t think of that! You’re actually good. I’ll try that rn

Still not working :frowning:

Here is the code:

UIS.InputBegan:Connect(function(Input, processed)
		if processed then return end

		if Input.KeyCode == Enum.KeyCode.Q then
			-- Check if the character exists
			if not Player.Character then return end
			if not Player.Character:FindFirstChild("Humanoid") then return end

			if Player.Character:GetAttribute("isRagdoll") == true then return end

			-- Get the Character directly from the player
			local md = Player.Character.Humanoid.MoveDirection.Magnitude
			if dashing or md <= 0 then return end

			local MoveDirection = camera.CFrame:VectorToObjectSpace(hum.MoveDirection)

			local left = math.round(MoveDirection.X) == -1
			local right = math.round(MoveDirection.X) == 1
			local front = math.round(MoveDirection.Z) == -1
			local back = math.round(MoveDirection.Z) == 1
		
		local forward = false
		local dashAnim
		
		
		if left then
			if deboune1 then return end
			deboune1 = true
			dashAnim = Animations.left
		end
		
		if right then
			if deboune1 then return end
			deboune1 = true
			dashAnim = Animations.right
		end
		
		if front then
			if debounce2 then return end
			debounce2 = true
			forward = true
			dashAnim = Animations.forward
		end
		
		if back then
			if deboune1 then return end
			deboune1 = true
			dashAnim = Animations.back
		end
		
		slowDash(forward, dashAnim) --code stops here from last check after the plr dies!!!
	end
end)

Then, give me the most important parts and info:

  • The parent of the script
  • The declaration part where you declare Player, hum and etc.

The parent is on StarterPlayer, StarterPlayerScripts

The declaration part:

task.wait(1.5)
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local groundModule = require(RS.RockScript)
local stundHandler = require(RS.StunHandlerV2)

local camera = workspace.CurrentCamera
local gameSetting = UserSettings().GameSettings
local Player = game.Players.LocalPlayer
local char = Player.Character
local hum = char.Humanoid
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

local deboune1 = false
local debounce2 = false
local dashing = char:SetAttribute("Dashing", false)

local cd1 = 3
local cd2 = 5

local Animations = {
	forward = "http://www.roblox.com/asset/?id=16628990362",
	back = "http://www.roblox.com/asset/?id=16589026693",
	left = "http://www.roblox.com/asset/?id=16589026693",
	right = "http://www.roblox.com/asset/?id=16589026693",
	punch = "http://www.roblox.com/asset/?id=16589026693"
}

So the problem is the one I’ve mentioned before:

-- The player reference is universal
local Player = game.Players.LocalPlayer
-- You reference only the first character for that player
-- Each life the NEW character will be created, so this one is wrong
local char = Player.Character
-- The same goes for Humanoid. Even more, it could not exist when the script
-- starts to run (if loading is long), so it may even throw an error
local hum = char.Humanoid
-- HumanoidRootPart is only for the first character too
local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")

The solution might be the following:

  1. Put the script into StarterCharacterScripts. It will run each time the new character appears.
  2. Never reference the character variables as you did. The correct approach of referencing will be:
local Player = game.Players.LocalPlayer
-- If the character didn't load, it will wait for CharacterAdded and only then
-- declare the character variable
local Character = Player.Character or (Player.CharacterAdded:Wait() and Player.Character)
-- Always wait for instances or some long loading will trash your scripts
local Humanoid = Character :WaitForChild("Humanoid")
local HumanoidRootPart = Character :WaitForChild("HumanoidRootPart")

Let me know if it works!

1 Like

Thank you a lot! I will try the code as soon as I am able, rn I can’t, I’ll mark you as SOLUTION if it works.

Still doesn’t work. Ty for you time.

If you still want to solve the problem here is the entire code:

task.wait(1.5)
local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local groundModule = require(RS.RockScript)
local stundHandler = require(RS.StunHandlerV2)

local camera = workspace.CurrentCamera
local gameSetting = UserSettings().GameSettings
local Player = game.Players.LocalPlayer
-- If the character didn't load, it will wait for CharacterAdded and only then
-- declare the character variable
local char = Player.Character or (Player.CharacterAdded:Wait() and Player.Character)
-- Always wait for instances or some long loading will trash your scripts
local hum = char :WaitForChild("Humanoid")
local HumanoidRootPart = char :WaitForChild("HumanoidRootPart")

local deboune1 = false
local debounce2 = false
local dashing = char:SetAttribute("Dashing", false)

local cd1 = 3
local cd2 = 5

local Animations = {
	forward = "http://www.roblox.com/asset/?id=16628990362",
	back = "http://www.roblox.com/asset/?id=16589026693",
	left = "http://www.roblox.com/asset/?id=16589026693",
	right = "http://www.roblox.com/asset/?id=16589026693",
	punch = "http://www.roblox.com/asset/?id=16589026693"
}



function createHitbox(hitBox)
	
	coroutine.resume(coroutine.create(function()
		
	hitBox.CanCollide = false
	hitBox.CanTouch = true
	hitBox.CanQuery = false
	hitBox.Transparency = 1
	hitBox.Anchored = false
	hitBox.Size = Vector3.new(5, 5, 5)
	hitBox.Parent = workspace

	local weld = Instance.new("Weld")
	weld.Part0 = char.PrimaryPart
	weld.Part1 = hitBox
	weld.Parent = char.PrimaryPart
	weld.C0 = CFrame.new()
	weld.C1 = CFrame.new()
		
	end))
end



function createEffect(forward)
	--local dash = RS["Gojo dash blue"]:Clone()
	--dash.Parent = workspace
	--dash.CFrame = char.Torso.CFrame
	--local weld = Instance.new("Weld")
	--weld.Part0 = char.Torso
	--weld.Part1 = dash
	--weld.Parent = char.PrimaryPart
	--weld.C0 = CFrame.new()
	--weld.C1 = CFrame.new()


	local sound = Instance.new("Sound")
	sound.SoundId = "rbxassetid://5989939664"
	sound.Volume = 1
	
	if forward then
		sound.PlaybackSpeed = 1.2 
	else
		sound.PlaybackSpeed = 1.465
	end
	
	sound.Parent = char.PrimaryPart
	sound:Play()
	
	coroutine.resume(coroutine.create(function()

		local effect = RS["growing wind"]:Clone()
		effect.Parent = workspace
		effect.CFrame = HumanoidRootPart.CFrame * CFrame.Angles(-86.887, -77.751, -107.067)

		task.wait(.15)
		game:GetService("TweenService"):Create(effect, TweenInfo.new(.1), {Transparency = 1}):Play()
		task.wait(.1)
		effect:Destroy()
	end))
end



function slowDash(forward, dashAnim)
	local bodyV = Instance.new("BodyVelocity", HumanoidRootPart)
	bodyV.MaxForce = Vector3.new(math.huge, 0, math.huge)
	
	local hitBox = Instance.new("Part")
	local bool = true
	dashing = char:SetAttribute("Dashing", true)

	if forward then
		
		if dashAnim ~= nil then
			local animation = Instance.new("Animation")
			animation.AnimationId = dashAnim
			local track = hum:LoadAnimation(animation)
			track:Play()
		end
		
		createEffect(forward)
		
		createHitbox(hitBox)
		
		
		for  i = 100, 16, -1.2 do
			if bool then
				
				hitBox.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") then
						hit.Parent.Humanoid.Health -= .05
						stundHandler.Stun(hit.Parent.Humanoid, .25)
						bool = false
					end
			end)
			
			bodyV.Velocity = HumanoidRootPart.CFrame.LookVector *i
			task.wait()
			end
		end
		
		task.delay(cd1, function()
			debounce2 = false
		end)
		
	else
		
		gameSetting.RotationType = Enum.RotationType.CameraRelative
		
		for i = 60, 16, -1 do
			
			local md = hum.MoveDirection
			bodyV.Velocity = Vector3.new(md.X * i, 0, md.Z * i)
			task.wait()
		end
		
		task.delay(cd1, function()
			deboune1 = false
		end)
	end
	
	gameSetting.RotationType = Enum.RotationType.MovementRelative
	bodyV:Destroy()
	dashing = char:SetAttribute("Dashing", false)
	forward = false
	
	if hitBox then
		hitBox:Destroy()
	end
	
end




UIS.InputBegan:Connect(function(Input, processed)
		if processed then return end

		if Input.KeyCode == Enum.KeyCode.Q then
			-- Check if the character exists
			if not Player.Character then return end
			if not Player.Character:FindFirstChild("Humanoid") then return end

			if Player.Character:GetAttribute("isRagdoll") == true then return end

			-- Get the Character directly from the player
		local md = Player.Character.Humanoid.MoveDirection.Magnitude
		print(Player.Character.Humanoid.Health)
			if dashing or md <= 0 then return end

			local MoveDirection = camera.CFrame:VectorToObjectSpace(hum.MoveDirection)

			local left = math.round(MoveDirection.X) == -1
			local right = math.round(MoveDirection.X) == 1
			local front = math.round(MoveDirection.Z) == -1
			local back = math.round(MoveDirection.Z) == 1
		
		local forward = false
		local dashAnim
		
		
		if left then
			if deboune1 then return end
			deboune1 = true
			dashAnim = Animations.left
		end
		
		if right then
			if deboune1 then return end
			deboune1 = true
			dashAnim = Animations.right
		end
		
		if front then
			if debounce2 then return end
			debounce2 = true
			forward = true
			dashAnim = Animations.forward
		end
		
		if back then
			if deboune1 then return end
			deboune1 = true
			dashAnim = Animations.back
		end
		
		slowDash(forward, dashAnim) --code stops here from last check after the plr dies!!!
	end
end)
	
	

Hey @TryingToCoDe2 !

I’ve tested the updated script on an empty server parented to StarterCharacterScripts (excluding your groundModule and stundHandler) and everything works fine before and after the character reset. If you still having issues, they are in your other connected scripts.

robloxapp-20240308-1846306.wmv (2.4 MB)

1 Like

Ty for you help a lot! Becasue of you I am able to finish the first version of my dash system!

1 Like

Was glad to help, @TryingToCoDe2 !

1 Like

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