Problem with entity spawning



hook.Add("InitPostEntity", "DoSpawn", function()
	timer.Create(30, function() --i tried with timer and without
		local Spawns = {}

		Spawns["anom_damage"] = Vector(-389.088867, 7833.652344, 82.448303)
		Spawns["anom_damage"] = Vector(4582.639648, 9960.668945, 44.784798)

		for k, v in pairs(Spawns) do
                        print("Spawned "..k)
			local anomaly = ents.Create(k)
			if !IsValid(anomaly) then return end
			anomaly:SetPos(v)
			anomaly:Spawn()
		end
	end)
end)


As for me this code seems ok but not works. The file is in lua/autorun/server/sv_spawner.lua. I tried to spawn via lua_run but again nothing works.

When i run this code via lua_run nothing shows too, i thought that entity spawns somewhere else, but no.



lua_run for k, v in pairs(ents.GetAll()) do if v:GetClass() == "anom_damage" then print(v) end end


That code you posted shouldn’t even work: you’re using the timer.Simple argument structure with timer.Create. That means the hook isn’t being added – make sure the file is actually being included and it’s being done serverside.

Also, since the vectors are constant, you can move them to a table outside of the hook.

Ok, done.
Now code looks like this:



local Spawns = {}
Spawns["anom_damage"] = Vector(-389.088867, 7833.652344, 82.448303)
Spawns["anom_damage"] = Vector(4582.639648, 9960.668945, 44.784798)

hook.Add("InitPostEntity", "DoSpawn", function()
	timer.Simple(30, function()
		for k, v in pairs(Spawns) do
			local anomaly = ents.Create(k)
			if !IsValid(anomaly) then return end
			anomaly:SetPos(v)
			anomaly:Spawn()
                        print("Spawned "..k)
		end
	end)
end)


Well, it spawns at least one entity. Thanks!
UPD: Lol i forgot to set up table in a proper way, it defines entity with first pos and then change ent’s pos to another.

[editline]6th August 2017[/editline]

Here’s fully working code:



local Spawns = {}
Spawns["DamageAnom1"] = {"anom_damage", Vector(-389.088867, 7833.652344, 82.448303)}
Spawns["DamageAnom2"] = {"anom_damage", Vector(4582.639648, 9960.668945, 44.784798)}

hook.Add("InitPostEntity", "DoSpawn", function()
	timer.Simple(30, function()
		for name, data in pairs(Spawns) do
			local anomaly = ents.Create(data[1])
			if !IsValid(anomaly) then return end
			anomaly:SetPos(data[2])
			anomaly:Spawn()
			print("Spawned "..name)
		end
	end)
end)