Auto-spawning NPCs

I am running a modified DarkRP, called ZombieRP, and I’m tired of going on long searches for maps with zombie spawns in them.

I found this on another thread, but its aimed at ZMod, and I tried it on a test server and didn’t work.
[lua]
–local numZombies = 0
–local maxZombies = 150

function ZombieSpawns()

 if ( numZombies < maxZombies ) then

      local zombie = ents.Create("npc_zombie")
      zombie:SetPos( Vector( -3744.217773, -1111.466675, 314.03125 ) )
      zombie:Spawn()
      numZombies = numZombies + 1

     timer.Simple( 10, ZombieSpawns )

end

end

hook.Add( “InitPostEntity”, “StartZombieSpawning”, ZombieSpawns )[/lua]

I turned it into a darkrp module, in the modification/lua/darkrp_module/zombiespawns/sv_zombiespawns.lua

When that didn’t work, I tried putting it into modification/lua/autorun/server/zombiespawns.lua

Any code help, and/or installation help would be much appreciated.

What I do, is spawn the initial npcs; on death of the npc I force a “respawn” to always ensure a specific number.

The NPCs aren’t spawning at all, that’s why I think it may be an installation issue, problem is, I have no idea where to put it.

Does it give any error? I’d recommend using SetModel( String model ), Activate( ), etc when spawning them…

No error has been given, which is the most frustrating part. I’m afraid I’m still fairly new at coding, and wouldn’t know how to end that string, or where to put it.

Why have you commented out numZombies and maxZombies which your code seems to be relying on?

It was a mistake that I noticed after posting, its fixed in the file itself.

Try the following code:

[lua]
local numZombies = 0
local maxZombies = 150

function ZombieSpawns()

timer.Simple( 5, function()

    if ( numZombies < maxZombies ) then

        local zombie = ents.Create("npc_zombie")
        zombie:SetPos( Vector( -3744.217773, -1111.466675, 314.03125 ) )
        zombie:Spawn()
        zombie:Activate()

        numZombies = numZombies + 1

    end

end )

end

hook.Add( “InitPostEntity”, “StartZombieSpawning”, ZombieSpawns )
[/lua]

It’s pretty much the same as the original but I changed the timer handling a bit differently and I added zombie:Activate(). I remember having to add a timer for an auto-spawn system I made a while ago for it to work. Give it a go, let me know how it goes.

Alright, thank you a lot, as for my other question that may be messing the entire thing up from the start, where do I install it? I ended up making an addon of sorts that just goes through addons/zspawn/lua/autorun/server/zombiespawn.lua

It does have the addon.txt in it.

Edit- That works, but how could I go about making it to where I can have multiple spawns setup, and when they are killed they respawn instantly?

Create a file in addons/zspawn/lua/autorun/ and call it something like sh_zombiespawn.lua and also for convenience, rename zombiespawn.lua in the autorun server file to sv_zombiespawn.lua. After that, add the following code to sh_zombiespawn.lua:
[lua]
AddCSLuaFile()

if ( SERVER ) then

include( "server/sv_zombiespawn.lua" )

end
[/lua]
Basically what that will do is ensure your code is executed.

Thank you so much for your help. But, when that was added, I got this lua error.



[ERROR] addons/zspawn/lua/autorun/sv_zombiespawn.lua:10: attempt to call field 'Create' (a nil value)
  1. unknown - addons/zspawn/lua/autorun/sv_zombiespawn.lua:10

Timer Failed! [Simple][@addons/zspawn/lua/autorun/sv_zombiespawn.lua (line 6)]


You do realize DarkRP has a zombie event module by default?

I don’t believe you placed the files correctly. You should have 2 main files.

File 1 - addons/zspawn/lua/autorun/sh_zombiespawn.lua:
[lua]
AddCSLuaFile()

if ( SERVER ) then

include( "server/sv_zombiespawn.lua" )

end
[/lua]

File 2 - addons/zspawn/lua/autorun/server/sv_zombiespawn.lua:
[lua]
local numZombies = 0
local maxZombies = 150

function ZombieSpawns()

timer.Simple( 5, function()

    if ( numZombies < maxZombies ) then

        local zombie = ents.Create( "npc_zombie" )
        zombie:SetPos( Vector( -3744.217773, -1111.466675, 314.03125 ) )
        zombie:Spawn()
        zombie:Activate()

        numZombies = numZombies + 1

    end

end )

end

hook.Add( “InitPostEntity”, “StartZombieSpawning”, ZombieSpawns )
[/lua]

I do, but they come in waves and its quite annoying to use. I was hoping to use something custom, like this. I’m also working on making a custom gamemode, and I needed a way to customize where zombies spawn.

@Shadow- That’s how I have it setup, and that’s the error I’m getting.

Edit- I just realized I removed the sv_zombiespawn.lua from lua/autorun/server to just autorun.

Editx2- The zombies do not respawn after killed, but they do spawn without any errors.

They aren’t respawning because there isn’t any code to make them respawn. You only have it set to spawn the npc once on InitPostEntity. Other than that, I’m glad it’s working.

What would the line(s) need to be added to make them respawn every “x” amount of seconds?

You could use timer.Create http://wiki.garrysmod.com/page/timer/Create
[lua]
local numZombies = 0
local maxZombies = 150

function ZombieSpawns()

timer.Simple( 5, function()

    timer.Create( "Respawner", 10, 10, function()

        if ( numZombies < maxZombies ) then

            local zombie = ents.Create( "npc_zombie" )
            zombie:SetPos( Vector( -3744.217773, -1111.466675, 314.03125 ) )
            zombie:Spawn()
            zombie:Activate()

            numZombies = numZombies + 1

        end

    end )

end )

end

hook.Add( “InitPostEntity”, “StartZombieSpawning”, ZombieSpawns )
[/lua]
That example uses your same code but utilises timer.Create to run the code 10 times, every 10 seconds. timer.Create( string identifier, number delay, number repetitions, function func )

Now, final question, how could I make it to where there are multiple spawn points?

You could use a table like this:
[lua]
local ZombieSpawnPoints = {

{ pos = Vector( 0, 0, 0 ) },
{ pos = Vector( 0, 0, 0 ) },
{ pos = Vector( 0, 0, 0 ) },

}

local numZombies = 0
local maxZombies = 150

function ZombieSpawns()

timer.Simple( 5, function()

    if ( numZombies < maxZombies ) then

        for k, v in pairs( ZombieSpawnPoints ) do

                local zombie = ents.Create( "npc_zombie" )
                zombie:SetPos( v.pos )
                zombie:Spawn()
                zombie:Activate()

                numZombies = numZombies + 1

        end

    end

end )

end

hook.Add( “InitPostEntity”, “StartZombieSpawning”, ZombieSpawns )
[/lua]

works perfectly, thanks so much!

[editline]20th May 2014[/editline]

Now I have the problem of they won’t respawn, or spawn more. No lua errors were given, I tried adding the timer.Create given in one of the scripts above, but it didn’t work at all. Sorry for all the trouble I’m causing :confused: