Whats the best way to handle lots of NPCs/Humanoids?

lets say i mad a Enemy that always walks towards your player, ill do this

while true do
Enemy:MoveTo(myplayer.Position)
wait()
end

but what if there was 100 enemies? would i do this?

while true do
     for i,enemy in pairs(enemies) do
          enemy:MoveTo(myplayer.Position)
          wait()
     end
end

would that be the best way to do it? will it cause lag? maybe coroutines?

1 Like

Both of those examples will freeze your computer. You need some

wait()

The second option works (in theory). You just need some waits :wink:

2 Likes

added the waits. there are waits now

Yeah, the code looks fine to me. You can add however long of a wait you want (wait() might be too fast, maybe wait(2)). I suggest just testing out, and seeing what works. I don’t see much of a problem with your code otherwise.