NPC pathfinding not constantly updating and SetNetworkOwner not preventing choppy movement

I want to make a function that lets you select NPCs you want to follow you.

When the NPC is following me they keep stopping at different positions and when they get close to me they can only be a few studs away. Video of the problem and code: https://i.gyazo.com/5c1f6e21679fa5c549aeb1aab0ed10f3.mp4

Remote.OnServerEvent:Connect(function(plr, NPC, Cap)
	local Char = plr.Character
	local Hum = NPC.Humanoid
	
	if #Following ~= Cap.Value and not table.find(Following, NPC) then
		table.insert(Following, NPC)
		NPC.PrimaryPart:SetNetworkOwner(nil)
		
		local function FollowPath()
			local Path = PathFinding:CreatePath()
			Path:ComputeAsync(NPC.PrimaryPart.Position, Char.PrimaryPart.Position)
			local Waypoints = Path:GetWaypoints()
			
			if Path.Status == Enum.PathStatus.Success then
				for i, w in ipairs(Waypoints) do
					Hum:MoveTo(w.Position)
					Hum.MoveToFinished:Wait()
				end
			end
		end
		
		local function Main()
			if Char.Humanoid.Health ~= 0 then
				FollowPath()
			else
				
			end
		end
		
		while wait() do
			if Hum.Health == 0 then
				break
			end
			Main()
		end
	else
		
	end
end)

I have already looked for solutions, but none helped.

I think that the problem is that the NPC is constantly waiting until it finished its old path. It seems like you are spamming this RemoteEvent. Why are you using a RemoteEvent here?

Not sure if it is really necessary to tweak the network ownership of the NPC. Also, you will probably have to set the network ownership of every part in the NPC and not just its PrimaryPart.

The remote event makes the selected npc follow you when you activate it once so it isn’t spammed, but I’ve already tried setting all of their base parts network ownership to nil with this but it never worked:

for i, v in pairs(NPC:GetDescendants()) do
	if v:IsA('BasePart') then
		if v:CanSetNetworkOwnership() then
			v:SetNetworkOwner(nil)
		end
	end
end

My bad, I forgot about the while loop.

Main will only finish when your NPC has traversed all waypoints. This is why it seems to be lagging behind.

Yeah the NPC is glitching, and lagging because you have several hundred instances of Follow and Main all running at the same time. /the set-up you have is not going to work.

Simply, you should get a Path, once. Go to the second waypoint. Repeat.

You should mod a pathfinding script, which works, to your liking. There may be hundreds in the Library which work. Here are a few:

SimplePath - Pathfinding Module - Resources / Community Resources - DevForum | Roblox

1 Like