Problem with rolling left or right

When I try to dash left or right, the character starts going forwards and then suddenly changes to left or right. Sometimes it goes forwards a bit, sometimes only forwards and sometimes only left or right. I just need someone to point me to the potential cause of the problem.

		local RS = game:GetService("RunService")
		local Debris = game:GetService("Debris")
		local RunService = game:GetService("RunService")
		local UserInputService = game:GetService("UserInputService")
		
		local player = game.Players.LocalPlayer
		local character = player.Character
		local humanoid = character:WaitForChild("Humanoid")
		local HRP = character:FindFirstChild("HumanoidRootPart")
		
		local keys = {
			["Forward"] = UserInputService:IsKeyDown(Enum.KeyCode.W),
			["Backwards"] = UserInputService:IsKeyDown(Enum.KeyCode.S),
			["Right"] = UserInputService:IsKeyDown(Enum.KeyCode.D),
			["Left"] = UserInputService:IsKeyDown(Enum.KeyCode.A)
		}
		
		local roll = {
			ForwardRoll = humanoid:LoadAnimation(script:WaitForChild("ForwardRoll")),
			BackwardsRoll = humanoid:LoadAnimation(script:WaitForChild("BackwardsRoll")),
			RightDash = humanoid:LoadAnimation(script:WaitForChild("RightDash")),
			LeftDash = humanoid:LoadAnimation(script:WaitForChild("LefttDash"))			
		}
		
		local duration = roll.ForwardRoll.Length
		
		local minYRotation = -20
		local maxYRotation = 20
		
		local _, pitch, _ = HRP.CFrame:ToOrientation()
		
		local bodyVelocities = {}
		
		local function Velocity(multiplier, velocity)
			multiplier = 0
			if keys["Forward"] then
				multiplier = 3.125
			elseif keys["Backwards"] then
				multiplier = -3.125
			elseif keys["Right"] then
				multiplier = 3.125
			elseif keys["Left"] then
				multiplier = -3.125
			end
			
			velocity = humanoid.WalkSpeed * multiplier
			
			return velocity
		end
		
		local calculatedVelocity = Velocity(0, keys)
		
		local function Roll()
			if humanoid.FloorMaterial ~= Enum.Material.Air then
				humanoid.UseJumpPower = false
				humanoid.AutoRotate = false
				
				local bodyVelocity = Instance.new("BodyVelocity")
				bodyVelocity.MaxForce = Vector3.new(99999,0,99999)
				bodyVelocity.Parent = HRP
				bodyVelocity.Velocity = HRP.CFrame.LookVector * calculatedVelocity * 10 / humanoid.WalkSpeed

				table.insert(bodyVelocities, bodyVelocity)
				
				while roll.ForwardRoll.isPlaying or roll.BackwardsRoll.isPlaying or roll.RightDash.isPlaying or roll.LeftDash.isPlaying  do
					local _, newPitch, _ = HRP.CFrame:ToOrientation()

					if pitch ~= newPitch then
						if #bodyVelocities > 0 then
							bodyVelocities[#bodyVelocities]:Destroy()
							table.remove(bodyVelocities, #bodyVelocities)
						end

						local bodyVelocity = Instance.new("BodyVelocity")
						bodyVelocity.MaxForce = Vector3.new(99999,0,99999)
						bodyVelocity.Parent = HRP
						
						if keys["Forward"] or keys["Backwards"] then
							bodyVelocity.Velocity = HRP.CFrame.LookVector * calculatedVelocity * 10 / humanoid.WalkSpeed
							print("a")
						elseif keys["Left"] or keys["Right"] then
							bodyVelocity.Velocity = HRP.CFrame.RightVector * calculatedVelocity * 10 / humanoid.WalkSpeed
							print("b")
						end

						table.insert(bodyVelocities, bodyVelocity)

					end

					wait(0.05)
				end


				bodyVelocities[#bodyVelocities].Velocity *= 0.9

				humanoid.AutoRotate = true
				humanoid.UseJumpPower = true

				for _, bodyVelocity in ipairs(bodyVelocities) do
					bodyVelocity:Destroy()
				end
			end
		end
		
		local function Shiftlocked()
			if keys["Forward"] then
				roll.ForwardRoll:Play()
				Roll()
				print("forward")
			elseif keys["Backwards"] then
				roll.BackwardsRoll:Play()
				Roll()
				print("backward")
			elseif keys["Right"] then
				roll.RightDash:Play()
				Roll()
				print("play")
			elseif keys["Left"] then
				roll.LeftDash:Play()
				Roll()
				print("play")
			end
		end
		
		if UserInputService.MouseBehavior == Enum.MouseBehavior.LockCenter then
			Shiftlocked()
		elseif UserInputService.MouseBehavior == Enum.MouseBehavior.Default or UserInputService.MouseBehavior == Enum.MouseBehavior.LockCurrentPosition then
			roll.ForwardRoll:Play()
			Roll()
		end
	end,

From what I see, this is not full code. And assuming it uses humanoid:LoadAnimation() it’s also a little bit outdated. Maybe provide us with a little bit more context?

It is the whole script, I just left out the part of the module script.

local Movement = {
(script)
}

return Movement

Yes, humanoid:LoadAnimation and BodyVelocity are a bit outdated but still work.

I have found a solution for my problem.

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