Npc Pathfinding Conflict pls Help

Hello Devs :wave:

soooo my Pathfinding works on stationary but not moving targets the problem is the movetofinished: because without it he repaths to the player however he wont be abel to go stairs properly and ignores cost and about cost he for some reason ignores that water is massively dangerous idk why so i wann ask waht should i do to keep movetofinished to make him walk smart and repath if stuck but also that my npc can properly chase players and not walk to the old position wihle keeping movetofinished

	local Moving = false
	local RepathCount = 0
	
	local function Movement(targetPosition,Running)
		if isStunned.Value then return end
		if Moving then return end

		task.spawn(function()

			Moving = true

			if Running == nil then
				hum:MoveTo(root.Position) -- cancel movement
				hum.WalkSpeed = 0
				StopMovementAnimations() -- to stop animation
				Moving = false
				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 * CharStats.SpeedIncreaseDecrease
				if WeaponStats.WeponClass == "Spear" then
					--IdleTrack.Priority = Enum.AnimationPriority.Action2
				end

				if not runTrack.IsPlaying then
					walkTrack:Stop()
					runTrack:Play()
					Movesound:Play()
					Movesound.PlaybackSpeed = 1.4
				end
			else
				hum.WalkSpeed = CharStats.BaseWalkSpeed * CharStats.SpeedIncreaseDecrease
				if WeaponStats.WeponClass == "Spear" then
					--IdleTrack.Priority = Enum.AnimationPriority.Action
				end

				if not walkTrack.IsPlaying then
					runTrack:Stop()
					walkTrack:Play()
					Movesound:Play()
					Movesound.PlaybackSpeed = 1

				end
			end

			

			local Path = Pathfinding:CreatePath({
				AgentRadius = 2,
				AgentHeight = 5,
				AgentCanJump = true,
				AgentCanClimb = true,
				AgentJumpHeight = 10,
				AgentMaxSlope = 45,
				cost = {
					CrackedLava = 2,
					Cobbelstone = 1,
					Brick = 1,
					Grass = 3,
					Ground = 2,
					Mud = 2,	
					Slate = 1,
					Rock = 2,
					LeafyGrass = 1,
					Climb = 0.5,
					ClimbHelper = 0.4,
					Stairs = 0.1,
					BridgeHelper = 0.1,
					DangerZone = math.huge,
					Water = math.huge,

				},
			})
			
			
			Path:ComputeAsync(root.Position, targetPosition.Position)
			


			if Path.Status == Enum.PathStatus.Success then
				local waypoints = Path:GetWaypoints()
				
				for _, waypoint in ipairs(waypoints) do
					local distance = (root.Position - targetPosition.Position).Magnitude
					Path:ComputeAsync(root.Position, targetPosition.Position)

					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
					
					hum:MoveTo(waypoint.Position)
					
					local Reached = hum.MoveToFinished:Wait(3) -- Thats the problem if i remove he stupid 

					if not Reached then
						Path:ComputeAsync(root.Position, targetPosition.Position)
						waypoints = Path:GetWaypoints()
						hum:MoveTo(root.Position)
						return
					end--if it stays he dosent follow player properly


					if distance >= 70 then -- and distance check also
						EnemyNpc = nil
						closestEnemy = nil
						closestDistance = math.huge
						detected = false
						Moving = false
						hum:MoveTo(root.Position)
						break
					end

					RunService.Heartbeat:Wait()
				end
				Moving = false
			else -- here is it if succes failed
				warn(Npc.Name .. " failed to pathfind.")
				Moving = false
				return
			end
			
			
			Moving = false
		end)
	end

has anyone an idea i literly read 20 forum post and tried it over and over agin and im just lost :skull:

For the water issue, I am pretty sure the reason is because you did “cost” instead of how you are supposed to pass it which is “Costs” in other words try doing

			Costs = {
				CrackedLava = 2,
				Cobbelstone = 1,
				Brick = 1,
				Grass = 3,
				Ground = 2,
				Mud = 2,	
				Slate = 1,
				Rock = 2,
				LeafyGrass = 1,
				Climb = 0.5,
				ClimbHelper = 0.4,
				Stairs = 0.1,
				BridgeHelper = 0.1,
				DangerZone = math.huge,
				Water = math.huge,
			},

Also you shouldnt use a 3 second timeout to determine its stuck and instead you should compare its current position with its start position and get the distance between them to determine whether or not its stuck

Also I thoroughly recommend you to use :MoveTo() method for moving towards the player to reduce the pauses you get instead of relying on MoveToFinished:Wait(3) but :MoveTo() will not fix repathing or stuck detection so you still need to check target movement or npc position changes

With the MoveToFinished:Wait()
it tells the npc to move to the location but it doesnt allow to change the location until the npc reaches the 1st location

so use a delay like MoveToFinished:Wait(0.3) and repath if the npc gets stuck, the target moved a few studs, or Path.Blocked fires

sorry for the extremly long delay things got in my way buuut i fixed it finally after finding out about path.Bloked funtion adn just did this Thanks plootus for mentioning it i never new that existet but to be honest its my fisrt time doing such a system :saluting_face::
local Moving = false
local RepathCount = 0

local function Movement(targetPosition,Running)
	if isStunned.Value then return end
	if Moving then return end

	task.spawn(function()

		Moving = true

		if Running == nil then
			hum:MoveTo(root.Position) -- cancel movement
			hum.WalkSpeed = 0
			StopMovementAnimations() -- to stop animation
			Moving = false
			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 * CharStats.SpeedIncreaseDecrease
			if WeaponStats.WeponClass == "Spear" then
				--IdleTrack.Priority = Enum.AnimationPriority.Action2
			end

			if not runTrack.IsPlaying then
				walkTrack:Stop()
				runTrack:Play()
				Movesound:Play()
				Movesound.PlaybackSpeed = 1.4
			end
		else
			hum.WalkSpeed = CharStats.BaseWalkSpeed * CharStats.SpeedIncreaseDecrease
			if WeaponStats.WeponClass == "Spear" then
				--IdleTrack.Priority = Enum.AnimationPriority.Action
			end

			if not walkTrack.IsPlaying then
				runTrack:Stop()
				walkTrack:Play()
				Movesound:Play()
				Movesound.PlaybackSpeed = 1

			end
		end

		

		local Path = Pathfinding:CreatePath({
			AgentRadius = 2,
			AgentHeight = 5,
			AgentCanJump = true,
			AgentCanClimb = true,
			AgentJumpHeight = 10,
			AgentMaxSlope = 45,-- that helped a lot too
			Costs = {
				CrackedLava = 2,
				Cobbelstone = 1,
				Brick = 1,
				Grass = 3,
				Ground = 2,
				Mud = 2,	
				Slate = 1,
				Rock = 2,
				LeafyGrass = 1,
				Climb = 0.5,
				ClimbHelper = 0.4,
				Stairs = 0.1,
				BridgeHelper = 0.1,
				DangerZone = math.huge,
				Water = math.huge,

			},
		})
		
		
		Path:ComputeAsync(root.Position, targetPosition.Position)
		


		if Path.Status == Enum.PathStatus.Success then
			local waypoints = Path:GetWaypoints()
			
			for _, waypoint in ipairs(waypoints) do
				local distance = (root.Position - targetPosition.Position).Magnitude
				Path:ComputeAsync(root.Position, targetPosition.Position)

				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
				
				hum:MoveTo(waypoint.Position)
				
				Path.Blocked:Connect(function()
					local Reached = hum.MoveToFinished:Wait(0.3) -- Thats the problem

					if not Reached then
						Path:ComputeAsync(root.Position, targetPosition.Position)
						waypoints = Path:GetWaypoints()
						hum:MoveTo(root.Position)
						return
					end
				end)


				if distance >= 130 then -- and distance check also
					EnemyNpc = nil
					closestEnemy = nil
					closestDistance = math.huge
					detected = false
					Moving = false
					hum:MoveTo(root.Position)
					break
				end

				RunService.Heartbeat:Wait()
			end
			Moving = false
		else -- here is it if succes failed
			warn(Npc.Name .. " failed to pathfind.")
			Moving = false
			return
		end
		
		
		Moving = false
	end)
end