Stuttering Pathfinding NPC

I am trying to make a zombie NPC which follows the player. The NPC works fine when there are no obstacles between the NPC and the player, where PathFindingService is not used. However the NPC starts to stutter when it runs the pathfinding code. Here is a video of the issue and the corresponding code:

robloxapp-20221230-2147010.wmv (2.8 MB)

function PathFinding(target)
	local path = game:GetService("PathfindingService"):CreatePath()
	path:ComputeAsync(zombie.HumanoidRootPart.Position,target.Position)
	local waypoints = path:GetWaypoints()
	
	for _,waypoint in ipairs(waypoints) do
		if CheckForLineOfSight(target) == true then
		repeat 
				zombieHumanoid:MoveTo(target.Position)
				path.Blocked:Connect(function()
					PathFinding(target) 
				end)
				
				if target == nil or target.Parent == nil then break end
				
				
			until CheckForLineOfSight(target) == false
			 break
		else
			zombieHumanoid:MoveTo(waypoint.Position)
			zombieHumanoid.MoveToFinished:Wait(1)
		end
		
			end
	end
while wait(0.1) do
	local target = FindNearestTorso()
	print(target)
	if target then
		PathFinding(target)
	end
end
2 Likes

Try zombieHumanoid.MoveToFinished:Wait() instead and if it persists, set the network ownership of every part to nil.

for _, part in pairs(bot:GetChildren()) do 
	if part:IsA("BasePart") then 
		part:SetNetworkOwner(nil) 
	end
end
3 Likes

I added the code but it still seems to be stuttering.

2 Likes

Update: Originally I inserted this code at the bottom, which did not solve the problem. However when I put it inside of the PathFinding() function, it somehow worked.

3 Likes

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