Dynamically De/Spawning SNPCs

So my goal is to create an enviroment mimicing a Zombie Apocolypse, so when youre outside, zombies are wandering the streets is the basic idea. Now clearly, too many will crash the server, but they have to be generated by the server. So to clarify, im not a complete newb at scripting, im a very talented C++ coder, but this is my first introduction to LUA. I udnerstand the setup and such but im no so farmilair with the ocmmands. So the basic idea is to create an array (table?) of locations on a map, and execute a function to spawn a zombie when a player (any players) is within x distance from it. Then, once the player is x distance away from the zombie, remove it to keep cpu usage low. So anyone willing to help out? Please dont bother replying if you dont intend on helping or if you want to charge a fee, im just looking to get into LUA. Thanks!

that’s pretty much it, what do you need help with?

I dont know how to determine if players are near an area, nor the commands to spawn entities? remove them?

you can use ents.Create to spawn them, checking if you are near the spawner with Ents.FindInSphere(or use a distance check). Then remove with Remove() using distance check to see if you’re far enough

Great, but why remove the spawner entity instead of the zombies? And is there such a thing as a ‘ForEach’ loop I can use once I use findInSphere to determine if the Entity is a player, because ovbiously I only want it to spawn a zombie if its a player thats nearby. I’m going to work on this tomorrow so if you can add me to steam that’d be great, thank you for the help!

oh you ment the zombies, yeah, that would be better.

A for loop or a numeric loop:

[lua]local Table = {“One”, “Two”, “Three” , “Four”}

for index,value in pairs(Table) do
print("Index: “…index…” | Value: "…value)
end

/* Output - Commonly Used
Index: 1 | Value: One
Index: 2 | Value: Two
Index: 3 | Value: Three
Index: 4 | Value: Four
*/

for i = 1,#Table do
print("Index: “…i…” | Value: "…Table*)
end

/* Output - A Lot Faster
Index: 1 | Value: One
Index: 2 | Value: Two
Index: 3 | Value: Three
Index: 4 | Value: Four
*/[/lua]

But its not going to be numeric, its going to be a table of entities.



SpawnZones = {}

function SpawnNPC(npc , loc)

if !SpawnZone[loc] then
char = ents.Create( npc )
char:SetPos( loc )
char:Spawn()
SpawnZone[loc] = true
MonitorNPC( char )
end

end


And if I use Ents.FindInSphere, it will return the entities that I need to check and see if they’re players.

Then do:

[lua]for index,entity in pairs(ents.FindInSphere(pos, radius)) do
if entity:IsPlayer() then
–do player stuff
end
end[/lua]