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

5 comments

0

It's probably because you're iterating through large tables so fast. What exactly is this script supposed to do? I can re-write it for you.

0

its a script to make a npc follow enemy npcs and kill them

0

I just realized that everytime it attacks it freezed

0

Its when it reaches the target it freezes

0

i just realized that the problem was a different script i had a healing script it activates everytime its health changes but it has no wait time so it goes on forever lol