When cloning NPCs, PathfindingService starts to break every time

After cloning an NPC, he first starts behaving strangely (turning sharply to the left), then when he is cloned for the 3rd time, he finally gets buggy (he waits for a very long time before the next waypoint for no reason). There are no errors in the output. I tried to solve this problem using SetNetworkOwner, but it didn’t help either.

here is my code (it’s a module script):

local function WalkTo(NPCHumanoid, Part, TalkingEnabled, DestroyEnabled)
	local NPC = NPCHumanoid.Parent
	
	Path:ComputeAsync(NPC.HumanoidRootPart.Position, Part.Position)
	local Waypoints = Path:GetWaypoints()
	for i, Waypoint in pairs(Waypoints) do
		if Waypoint.Action == Enum.PathWaypointAction.Jump then
			NPCHumanoid.Jump = true
		end
		NPCHumanoid:MoveTo(Waypoint.Position)
		NPCHumanoid.MoveToFinished:Wait()
	end
	NPCHumanoid:MoveTo(Part.Position)
	NPCHumanoid.MoveToFinished:Wait()
	
	coroutine.wrap(function()
		local sucess, errorMsg = pcall(function()
			task.wait(math.random(3, 5))
			if NPC.Config.WindowJumpscare.Value == true then
				NPC.Config.WindowJumpscare.Value = false
				WindowJumpscareAnimation(NPCHumanoid, "rbxassetid://102073471225249")
			end
		end) 
	end)()
	
	if DestroyEnabled == true then
		NPC:Destroy()
	end
	
	if TalkingEnabled == true then
		task.wait(0.5)
		TweenRotatingNPC(NPC.HumanoidRootPart)
		task.wait(0.2)
		NPCTalking(NPC)
	end
end

Possible Issues with PathfindingService After Cloning NPCs

When cloning NPCs, there may be residual data or inconsistencies that are not properly reset, leading to unexpected behavior such as sharp turns and long pauses at waypoints. Here are some common problems and solutions:

  1. Cloning Path Instances:

    • Ensure that the Path instance is not cloned or reused improperly. Each NPC should have a freshly computed path using Path:ComputeAsync(...) to avoid conflicts between different NPCs.
  2. Humanoid Root Part Misalignment:

    • Cloning might cause minor misalignments in the HumanoidRootPart’s position or orientation, leading to incorrect path calculations.
    • Solution: Reset the position and orientation of the cloned NPC’s HumanoidRootPart after cloning. For example:
      local rootPart = NPC.HumanoidRootPart
      rootPart.CFrame = CFrame.new(rootPart.Position)  -- Reset orientation
      
  3. Network Ownership:

    • Network ownership can affect how NPCs move and interact, especially if there are multiple NPCs being controlled by the server.
    • Solution: Explicitly set the network ownership of the NPC’s HumanoidRootPart to the server:
      NPC.HumanoidRootPart:SetNetworkOwner(nil)
      
  4. PathfindingService Overuse or Overlap:

    • If path calculations are too frequent or overlapping (e.g., rapid cloning), it might cause issues with pathfinding accuracy and performance.
    • Solution: Introduce a cooldown or debounce mechanism to control the frequency of path calculations:
      if not NPC._lastPathCompute or (tick() - NPC._lastPathCompute) > 1 then
          NPC._lastPathCompute = tick()
          Path:ComputeAsync(NPC.HumanoidRootPart.Position, Part.Position)
      end
      
  5. Memory Leaks and State Corruption:

    • Cloning NPCs without proper cleanup might lead to memory leaks or state corruption.
    • Solution: Ensure that any existing event connections (e.g., MoveToFinished) are properly disconnected or that cloned NPCs are entirely independent.
  6. Debugging with Waypoints:

    • To debug why NPCs pause at waypoints, log each waypoint position and check if any conditions (e.g., obstacles, collisions) are causing delays:
      for i, Waypoint in pairs(Waypoints) do
          print("Moving to Waypoint:", Waypoint.Position)
          if Waypoint.Action == Enum.PathWaypointAction.Jump then
              NPCHumanoid.Jump = true
          end
          NPCHumanoid:MoveTo(Waypoint.Position)
          NPCHumanoid.MoveToFinished:Wait()
      end
      

By addressing these areas, you should be able to mitigate the pathfinding issues when cloning NPCs in your game.

1 Like

It worked! Thank you very much

No worries :slight_smile:
Enjoy developing your game :hammer:

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