I am trying to make an NPC follow the nearest player it can find, however it lags a lot and i dont want this to happen, please help, my code:
Distance = 250
while wait() do
for i,Model in pairs(game.Workspace:GetChildren()) do
if Model.ClassName == "Model" and Model ~= script.Parent then
Humanoid = Model:FindFirstChild("Humanoid")
if Humanoid ~= nil then
if Humanoid.Health > 0 and Humanoid.Parent.Name ~= script.Parent.Name then
Torso = Humanoid.Parent:FindFirstChild("Torso")
if Torso ~= nil then
if (Torso.Position - script.Parent.Torso.Position).Magnitude < Distance then
script.Parent.Humanoid:MoveTo(Torso.Position)
end
end
end
end
end
end
end

2 comments
When is this script meant to be triggered?
This script is meant to go inside an NPC to make it follow a player, it triggers instantly and works but lags and I can't figure out why