Npc Cant walk right

Hello :wave:
SO in my Npc Script i made a walking and a Face target (to look at the enemy or attackebal part) function and well turning to the enmy works but walking is weird and very stacking like anmation laping over each other for some reason caused by face target if i loop it to face the enemy:

Movment Function

	local function Movement(targetPosition,Running)
		
		if Running == nil then
			hum:MoveTo(root.Position) -- cancel movement
			hum.WalkSpeed = 0
			StopMovementAnimations() -- to stop animation
			return
		end
		
		
		
		if Npc.Torso:FindFirstChild("MoveSound") then
			Movesound = Npc.Torso.MoveSound
			Movesound.Looped = true
			Movesound.Volume = 0.3
			
		else
			Movesound = Instance.new("Sound")
			Movesound.Looped = true
			Movesound.Parent = Npc.Torso
			Movesound.Volume = 0.3
			Movesound.Name = "MoveSound"
			Movesound.SoundId = "rbxassetid://1181265898"
		end
		
		

		
		if Running then
			hum.WalkSpeed = CharStats.SprintSpeed
			if not runTrack.IsPlaying then
				walkTrack:Stop()
				runTrack:Play()
				Movesound:Play()
				Movesound.PlaybackSpeed = 1.4

			end
		else
			hum.WalkSpeed = CharStats.BaseWalkSpeed
			if not walkTrack.IsPlaying then
				runTrack:Stop()
				walkTrack:Play()
				Movesound:Play()
				Movesound.PlaybackSpeed = 1

			end
			
		end
		

		
		if moving then return end
		moving = true
		
		
		local Path = Pathfinding:CreatePath({
			AgentRadius = 2,
			AgentHeight = 5,
			AgentCanJump = true,
			AgentCanClimb = true,
			cost = {
	
				CrackedLava = 2,
				Cobbelstone = 1,
				Brick = 1,
				Grass = 2,
				Ground = 1,
				Mud = 1,	
				Slate = 1,
				Rock = 2,
				LeafyGrass = 1,
				Water = math.huge,
				
			},
		})
		Path:ComputeAsync(Npc.HumanoidRootPart.Position, targetPosition)
		local waypoints = Path:GetWaypoints()
		
		

		if Path.Status == Enum.PathStatus.Success then
			local waypoints = Path:GetWaypoints()
			task.spawn(function()
				for _, waypoint in ipairs(waypoints) do
					local part = Instance.new("Part")
					part.Material = "Neon"
					part.Anchored = true
					part.CanCollide = false
					part.Shape = "Ball"
					part.Position = waypoint.Position
					part.Parent = game.Workspace
					
					if waypoint.Action == Enum.PathWaypointAction.Jump then
						hum.Jump = true
					end
					--FaceTarget(EnemyNpc.HumanoidRootPart)
					hum:MoveTo(waypoint.Position)
					RunService.Heartbeat:Wait()
				end
			end)
			
		else
			warn(Npc.Name .. " failed to pathfind.")
		end
		
		moving = false
	end

and thats my face target

	local function FaceTarget(target)
		if not root or not target then return end
		local targetPos = target.Position
		local dir = (targetPos - root.Position)
		dir = Vector3.new(dir.X, 0, dir.Z)
		if dir.Magnitude < 1 then return end

		local lookCF = CFrame.lookAt(root.Position, root.Position + dir)
		root.CFrame = lookCF
	end

i hope anyone has an idea what causes this because i have no clue and hope i can fix it soon

Try adding
hum.MoveToFinished:Wait()
after your MoveTo

It could also be a problem with the code calling the function, if you’d be willing to show that

1 Like

well i call face target like:

		if EnemyNpc and isHolding == false then
			isHolding = true -- for wepon holding animation
			IdleTrack:Play()
			
			task.spawn(function()
				while  task.wait() do
					if EnemyNpc then
						FaceTarget(EnemyNpc.HumanoidRootPart)
						if not EnemyNpc or EnemyNpc.Humanoid.Health <= 0 then
							EnemyNpc = nil
							break
						end
						
					elseif EnemyPart  then
						FaceTarget(EnemyPart)
						if not EnemyPart or EnemyPart.Health.Value <= 0 then
							EnemyPart = nil
							break
						end

					end
					if hum.Health <= 0 then	break end


				end

			end)

		end

and move to:

			local distance = (root.Position - EnemyNpc.HumanoidRootPart.Position).Magnitude



			if EnemyNpc.Humanoid.Health <= 0 and not detected then
				State = "Idle"
				EnemyNpc = nil
			end
			

			if distance >= 40 and not moving then
				Movement(EnemyNpc.HumanoidRootPart.Position, true) -- run
				return
			end

			if distance >= WeaponStats.Range and not moving then
				Movement(EnemyNpc.HumanoidRootPart.Position, false) -- walk
				return
			end

			if distance <= WeaponStats.Range then
				moving = false
				Movement(root.Position, nil)
			end

quick note its a collction service script with all npc inside that runs on that logic and well:

local function SetupNPC(Npc)
	task.defer(function()
		local hum = Npc:WaitForChild("Humanoid")
		local root = Npc:WaitForChild("HumanoidRootPart")


		while Npc.Parent and hum and hum.Health > 0 do
			NPCLogic(Npc) -- starts the logic do to it not being looped inside
			task.wait(0.5)
		end
	end)
end

well my script is a bit complicated after all :skull:

1 Like

You said this issue only arises when you are calling the function to make the NPC face the thing it’s attacking, right? I’m pretty sure changing a rigs position while it’s moving will end the MoveTo right away, so the only solution might be to find another way to make the enemy face the player.

Try using an Align Orientation object

And dont forget to try adding .MoveToFinished:Wait() if you haven’t already

i will look tomorrow because now its a bit late and inform myslv properly but thanks

1 Like

Try to set NPC’s network ownership to nil

How often are you calling Path:ComputeAsync? Because if you’re repeatedly calling it really fast, then I don’t doubt that’ll cause weird movement as shown in the video. Computing a path is not that cheap.

If you are doing that, then you need to find a more efficient way of creating movement such as by simply using :MoveTo when there’s no obstacles between the target and the enemy, and only computing paths if there obstacles between two subjects and the threshold for change of distance is met (just so you don’t have to calculate the path every time the player moves).

And yeah, try also setting the NPC’s network ownership to nil.

Looks like it’s doing what you told it to do.. Move to that waypoint less than a step away. You end up with a shuffle.

So currently im sorry fro this long response had other things to do i am currently tring to fix somthing else and still try to Research a bit because like Vsauce Vursice mentioned i call it way to often so i need to relook into that too and have still a lot to do because i only have like 2 hour max for developing but still thanks for the replies and help i hope i get it runnning a soon as possibel and keep track of other problems

Thank you
:saluting_face: