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
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: