function gethumanoids() local humanoidpos = {} for i,x in pairs(workspace.NPCS.Outriders:GetChildren()) do if x:FindFirstChild("Humanoid") and x~= script.Parent then table.insert(humanoidpos,(script.Parent.HumanoidRootPart.Position - x.HumanoidRootPart.Position).magnitude) end end table.sort(humanoidpos) for i,x in pairs(humanoidpos) do if x ~= 1 then table.remove(humanoidpos,i) end end for i,x in pairs(workspace.NPCS.Outriders:GetChildren()) do if x:FindFirstChild("Humanoid") and x ~= script.Parent then if (script.Parent.HumanoidRootPart.Position - x.HumanoidRootPart.Position).magnitude == humanoidpos[1] then return x end end end end while wait() do local target = gethumanoids() script.Parent.Humanoid:MoveTo(target.HumanoidRootPart.Position) end
I don't know why my game crashs like 2 seconds after starting
Its a script for a friendly NPC fighting nearest enemy NPCS