NPCs Stuttering while moving

What do you want to achieve?

I want my NPCs to move to each waypoint without pausing for a second per one.

What is the issue?

My NPCs will be fine for the first minuet of the game. But Later into the game they tend to stop at each waypoint. I have my pathfinding all in one function. Sometimes when the function runs, there’s no problem at all. Other times, they can pause for up too 2 seconds per waypoint. In some cases the NPC has exploded. and mangled itself?

What solutions have you tried so far?

Ive tried, Setting the Network ownership of all the baseparts to Nil,

Changing the wait between waypoints from a repeat wait() until Distance. To Humanoid.MoveToFinished:Wait(),

Having the NPCs cloned from Replicated storage instead of Lighting,

Using RunService.Heartbeat:Wait() Instead of just Wait(),

Checking other forum posts to find a solution but either it wasn’t the same problem or they had no solution.

Extra info

Then NPC is 1.5x the standered NPC proportions so hes a little bit chonk

Most of the paths are small and straight lines with little waypoints.

I’ve changed it so the waypoint height is always Torso height. as the NPC is always walking on a flat surface more or less in a straight line (This is because the waypoints spawn at the feet and sometimes go underground?)

CODE:

function Travel(Position)

	--Create Path

	local PathParams = {AgentRadius = 2.5, AgentHeight = 9, AgentCanJump = false ,WaypointSpacing = 3}
	local Path = PathfindingService:CreatePath(PathParams)
	local success, errorMessage = pcall(function()
		Path:ComputeAsync(script.Parent.HumanoidRootPart.Position, Position)
	end)
	local WayPoints = Path:GetWaypoints()

	--MoveToPoint

	WalkAnimationTrack:Play()

	wait()

	for i = 1,#WayPoints do	

		local WaypointPosition = WayPoints[i].Position
		local NewPosition = Vector3.new(WaypointPosition.X,script.Parent:WaitForChild("HumanoidRootPart").Position.Y, WaypointPosition.Z)

		script.Parent.Humanoid:MoveTo(NewPosition)

		repeat 			
			
			local distance = (NewPosition - script.Parent:WaitForChild("HumanoidRootPart").Position).magnitude

			RunService.Heartbeat:Wait()

		until distance <= 2.5 

	end

	script.Parent.humanoid:MoveTo(Position)

	if Path.Status == Enum.PathStatus.NoPath then
		warn("No Path")
	elseif Path.Status ~= Enum.PathStatus.NoPath then
		print("Successful Travel")
	end	

	WalkAnimationTrack:Stop()

end

Please help me, Ive been stuck on this for weeks!

Regards ~ Anon

1 Like

Try doing

script.Parent.HumanoidRootPart:SetNetworkOwner(nil)

At the top of the script

Already had a function that did it. but I switched it with the line you provided and it still doesnt work.

This is the function.

for p = 1,#InhabitantParts do 
	if InhabitantParts[p].ClassName == "MeshPart" or InhabitantParts[p]:IsA('BasePart')then
		InhabitantParts[p]:SetNetworkOwner(nil)
	end
end

Maybe try lowering 2.5 to something lower?

I’ve tried doubling it and halfling it. I even created a part at each waypoint to visualise it.

With Low distance (1) only 1 is spawning as the NPC is not moving towards it for the loop to restart

At a high distance (5) three is spawning because the first 2 are already in range and the third is out of range. But the NPC did not move towards any of them. He is still stuck in place. (For 18 seconds then moved to the next one XD)

for i = 1,#WayPoints do	
		
		local WaypointPosition = WayPoints[i].Position
		local newPosition = Vector3.new(WaypointPosition.X,Inhabitant:WaitForChild("HumanoidRootPart").Position.Y, WaypointPosition.Z)
		

		local Part = Instance.new("Part")
		Part.Parent = game.Workspace
		Part.Anchored = true
		Part.CanCollide = false
		Part.Position = newPosition
		Part.Size = Vector3.new(1,1,1)
		Part.Name = tostring(i)

Another question is when my character travels on flat terrain and sometimes a Flat part. Why isnt the height of the waypoint consistent. it always goes underground or floats?

I feel like it’s this part. If there’s a slope, then it will take longer to get within a 2.5 distance, and sometimes may never. Just use the position of the waypoint.

The Y position of the way-point just does what it wants
on a completely flat surface it will go under it. or like 20 studs above it if threes a platform above it.
(Mainly at the end/start but sometimes in the middle)

I’ve come to the conclusion that its a problem with either these things:

  1. When trying to scale the NPC to 1.5x there’s something I’ve missed so it still thinks its regular size.
  2. The game is too slow so the wait function in the Repeat loop is too long
  3. Its not ignoring non-collidable parts properly
  4. There’s a better way of writing the Pathfinding function more suited for what I need.