How to improve this script

So i have an enemy NPC that i want to start following the player when the player gets close

This is what i have currently (the relevant part of the script)

while task.wait(1) do
   if target then
      hum:MoveTo(target.Position)
   end
end

The reason i put it in a loop, is because if i dont, and player changes position when NPC starts following him, NPC will just go to the position where he saw the player and stop there, and not actually follow the player.

But i will have around 100 NPCs in 1 place, so i want everything to be optimized, and im trying to avoid while loops as much as possible. So question is, how can i optimize this code or write a better one?

You should have one runservice loop and in the runservice looop you should loop through a folder with npcs that will constantly move to the position

Detect when target moves and do :MoveTo(). Probably not the best way but it’s better than a loop

That will be a lot worse than what i have now. Im just doing a simple action every second, if i do it your way i will be looping through objects every frame

Not better than a while wait(1) loop. Because if player starts moving, instead of each second, it will again be calling the action each frame

Its not worse because its not seperate rbxconnections but rather one that handles everything, I do this in my game and there are no fluctuations in memory at all so idk what you mean by its worse

Maybe add a cooldown of like 1/4 a second then. idk