Timer Failed on Script

Im really new to Gmod Lua and I wanted a script that can set loot spawns so I found one and I ran it after modifying it to how I liked it and I get the error in the console:

[ERROR] lua/new.lua:8: Tried to use a NULL entity!

  1. SetPos - [C]:-1
  2. unknown - lua/new.lua:8

Timer Failed! [Spawnitem][@lua/new.lua (line 13)]

And here’s my code:

itemsTable = {“item_healthkit”, “sent_siku_crate”, “item_ammo_357”, “item_ammo_ar2”, “item_ammo_ar2_altfire”, “combine_mine”, “item_ammo_crossbow”, “item_healthvial”, “item_ammo_pistol”, “item_rpg_round”, “item_box_buckshot”, “item_ammo_smg1”, “item_battery”, “weapon_bfg_beta_spas”, “weapon_bfg_assasin_pistols”, “weapon_bfg_hmg”, “weapon_bfg_iceaxe”, “weapon_bfg_oicw”, “weapon_bfg_mp7”, “weapon_bfg_smg2”, “weapon_bfg_hl2_sniper”, “weapon_bfg_ar1”}

local poses = {Vector(228,134,135), Vector(1337,1337,1337), Vector(666,666,666)}
local itemstospawnpertime = 2
local timebetweenspawns = 5

function DoSpawnItem()

for i = 1, itemstospawnpertime do
local item = ents.Create( table.Random( itemsTable ) )

item:SetPos( table.Random(poses) )
item:Spawn()

end
end
timer.Create(“Spawnitem”, timebetweenspawns, 0, DoSpawnItem)

I can give more details when needed.

don’t do table.Random( itemsTable ), because it returns the object and then index, you have to do
local class, _ = table.Random( itemsTable )
and then spawn the class

You don’t seven have to do table.Random – you an do:


itemsTable[math.random(1, #itemsTable)]

Also, in the future, use local variables, and


 tags when posting on the forums.

@code_gs Like this? Because im still getting errors.


local item = itemsTable[math.random(1, #itemsTable)]
	
	item:SetPos( table.Random(poses) )
	item:Spawn()

You can do the same sort of logic for your “poses” random selection, instead.

It’s working now, thank you SO much! :happy: