Lua Timer Error and Crashing

I hope that this is in the right section so please let me know if it isn’t. Anyway I’m new to lua coding and I’m having problems with a timer function. Every time I load the script I get an error saying: “Timer Error: [@lua\includes\extensions able.lua:177] bad argument #2 to ‘random’ (interval is empty)”. Here’s the code:



timer.Create("Spawn", 10, TotalZombies, SpawnNPC)

function SpawnNPC()
	SpawnPoints = ents.FindByName("Zombie_Spawn") //Finds all zombie spawn points (info_targets with the specified name)
	SpawnPoint = table.Random(SpawnPoints) //Picks a random spawn point for the zombie to spawn at
             zombie = ents.Create("npc_zombie")
	zombie:SetPos(SpawnPoint:GetPos()) //Sets the spawn point for the zombie to spawn at
	zombie:SetName("zombie")
	zombie:SetKeyValue( 'wakeradius' , '25000.00' )
	zombie:SetKeyValue( 'ignoreunseenenemies' , '0' )
	zombie:SetKeyValue( 'sleepstate' , '1' )
	zombie:Spawn()
end


Each time the SpawnNPC function is loaded the error will appear but a zombie will spawn regaurdless of the error which is annoying and when a zombie spawns it just stands there even though the keyvalue was set so they will not ignore unseen enemies and the wake radius is the size of the map (you can look these keyvalues up here under keyvalues: https://developer.valvesoftware.com/wiki/Npc_zombie ). I’m quite sure the crash is just because the zombies don’t move and spawn ontop of eachother because it dosn’t happen when I move them. Is there a better way to make them attack a player anywhere on the map on spawn and not crash the game? And is there any reason for the error and how could I fix it?

There are no entities “Zombie_Spawn” found so the table that ents.FindByName is supposed to fill is empty. Try fixing that first.

Well on the specific map I made for testing this does have a “Zombie_Spawn” info_target on it and two to be exact. Maybe cause theres two? But it’s supposed to find all entities with that name and index them into a table and then pick one randomly. It then spawns the zombie right where I want it to when I test the script yet for some reason there’s an error. So realy, it works but it dosn’t. That and the NPC’s not moving on spawn is what’s pissing me off.

print spam, that’s the best way to find your error.

After every line where you change a value print that value so for testing your could would look like,

[lua]
function SpawnNPC()
SpawnPoints = ents.FindByName(“Zombie_Spawn”) //Finds all zombie spawn points (info_targets with the specified name)
print("SpawnPoints is a " … tostring(SpawnPoints))
PrintTable(SpawnPoints) – This line will fail if SpawnPoints isn’t a table
SpawnPoint = table.Random(SpawnPoints) //Picks a random spawn point for the zombie to spawn at
print("Selected Spawn Point: " … tostring(SpawnPoint))
zombie = ents.Create(“npc_zombie”)
zombie:SetPos(SpawnPoint:GetPos()) //Sets the spawn point for the zombie to spawn at
zombie:SetName(“zombie”)
zombie:SetKeyValue( ‘wakeradius’ , ‘25000.00’ )
zombie:SetKeyValue( ‘ignoreunseenenemies’ , ‘0’ )
zombie:SetKeyValue( ‘sleepstate’ , ‘1’ )
zombie:Spawn()
print(zombie)
end
[/lua]

Then check the console to see what values you are getting, then you’ll know if the function is returning the entities you are expecting.

Thank you for the help. I actualy figured out that that was a client side error and the script is shared so it only works for the serverside part. I’ll probably just make the whole script serverside to prevent more errors scince people get annoyed by it alot. I’ll make another thread on how to make npcs attack a player from anywhere scince this one is mainly about the error.