ents.Create() Spawn Limit

They will spawn infinitely!!!
How to make limited spawn NPC?!



local NPC = {}

NPC.Conf = {
	npc_mutant_dog = {
		pos = { -- NPC positions

			[1] = {
				pos = Vector(-4256.836426, -6920.760254, -155),
				angle = Angle(0, 0, 0),
			},

			[2] = {
				pos = Vector(-4356.836426, -6920.760254, -155),
				angle = Angle(0, 0, 0),
			},

			[3] = {
				pos = Vector(-4256.836426, -7020.760254, -155),
				angle = Angle(0, 0, 0),
			}

		},
		limit = 4
	},
	npc_mutant_cat = {
		pos = {
 
			[1] = {
				pos = Vector(-4700.508301 -5286.329102 -180),
				angle = Angle(0, 0, 0),
			},

			[2] = {
				pos = Vector(-4700.508301 -5386.329102 -180),
				angle = Angle(0, 0, 0),
			},

			[3] = {
				pos = Vector(-4800.508301 -5286.329102 -180),
				angle = Angle(0, 0, 0),
			}

		},
		limit = 3
	},
	npc_mutant_rodent = {
		pos = {

			[1] = {
				pos = Vector(-2363, -12824, -589),
				angle = Angle(0.000, 90.000, 0.000),
			}

		},
		limit = 1
	},
	npc_mutant_controller = {
		pos = {

			[1] = {
				pos = Vector(424.93, -2542.29, -895.97), -- 443.066559 -2538.139404 -831.968750;setang 3.157424 117.809540 -0.000000
				angle = Angle(0, 109.29, 0),
			}

		},
		limit = 1
	},
	npc_mutant_pseudogiant = {
		pos = {

			[1] = {
				pos = Vector(-183.32, -1266.97, -639.97), -- -252.307831 -1332.139282 -575.968750;setang 2.368048 -170.098373 0.000000
				angle = Angle(0, -179.21, 0),
			}

		},
		limit = 1
	},
	npc_mutant_snork = {
		pos = {

			[1] = {
				pos = Vector(-1663.16, -1646.54, -639.97), -- -1552.668457 -2253.042725 -575.968750;setang 26.413170 146.313461 0.000000
				angle = Angle(0, -134.61, 0),
			},

			[2] = {
				pos = Vector(-1790, -1576.29, -639.97), -- -1552.668457 -2253.042725 -575.968750;setang 26.413170 146.313461 0.000000
				angle = Angle(0, -63.55, 0),
			},

			[3] = {
				pos = Vector(-1258.6, -2607.25, -639.97), -- -1552.668457 -2253.042725 -575.968750;setang 26.413170 146.313461 0.000000
				angle = Angle(0, 131.87, 0),
			},

			[4] = {
				pos = Vector(-334.75, -2644.07, -639.97), -- -1552.668457 -2253.042725 -575.968750;setang 26.413170 146.313461 0.000000
				angle = Angle(0, 93.51, 0),
			}

		},
		limit = 4
	},
	npc_mutant_classiczombie = {
		pos = {

			[1] = {
				pos = Vector(-470.32, -2453.75, -639.97), -- -1552.668457 -2253.042725 -575.968750;setang 26.413170 146.313461 0.000000
				angle = Angle(0, 151.25, 0),
			},

			[2] = {
				pos = Vector(-480.69, -2291.19, -639.97), -- -1552.668457 -2253.042725 -575.968750;setang 26.413170 146.313461 0.000000
				angle = Angle(0, 151.25, 0),
			},

			[3] = {
				pos = Vector(-1377.57, -1824.16, -639.97), -- -1552.668457 -2253.042725 -575.968750;setang 26.413170 146.313461 0.000000
				angle = Angle(0, -89.43, 0),
			}

		},
		limit = 3
	},
	npc_mutant_chimera = {
		pos = {

			[1] = {
				pos = Vector(434.434937, 2926.948242, -52.664200), -- -1552.668457 -2253.042725 -575.968750;setang 26.413170 146.313461 0.000000
				angle = Angle(0, 0, 0),
			}

		},
		limit = 1
	}
}

if(SERVER)then

local Spawned = {}

timer.Create("NPCSpawner", 1000, 0, function()
	for class, val in pairs(NPC.Conf)do
		for _, v in pairs(val.pos)do
			local npc = ents.Create(class)
			npc:SetPos(v.pos)
			npc:SetAngles(v.angle)
			npc:Spawn()
			npc:Activate()

			val.cur = {}

			if(!table.HasValue(val.cur, npc))then
				table.insert(val.cur, npc)
				print(table.Count(val.cur).."/"..val.limit.." - "..class)
			end
			if!(IsValid(npc))then
				npc:Remove()
				table.RemoveByValue(val.cur, npc)
			end
			if(table.Count(val.cur) > val.limit)then
				npc:Remove()
				table.RemoveByValue(val.cur, npc)
			end
		end
	end
end)

end




	local function spawnNextbot()
		local nextbot = ents.Create("nextbot")
		if game.GetMap() == "gm_flatgrass" then
			nextbot:SetPos(Vector(64, 7, -12223.968750))
		elseif game.GetMap() == "rp_downtown_v4c_v4" then
			nextbot:SetPos(Vector(3256.968750, 3352.385498, -131.968750))
			nextbot:SetAngles(Angle(-0, -180, 0))
		end
		nextbot:Spawn()
		nextbot:Activate()
	end

	local numToSpawn = 2

	if game.GetMap() == "gm_flatgrass" then
		numToSpawn = 5
	end

	hook.Add("InitPostEntity", "car_dealer_nextbot", function()
		for i=1,numToSpawn do
			timer.Simple((i-1)*5, spawnNextbot)
		end
	end)
	hook.Add("PostCleanupMap", "car_dealer_nextbot", function()
		for i=1,numToSpawn do
			timer.Simple((i-1)*5, spawnNextbot)
		end
	end)


I used this piece of code. Might not be the best way to do it but it gives you an example of they way I did it with my nextbot. Obviously, you will have to change it to what you want.

Haven’t read your code soo closely but I noticed you have 0 as the third argument in your timer.Create. This will make it an infinite loop, replace 0 with 1 to make it call once.

I need them to spawn every time,
in order to ensure that the NPC limit is not exceeded…

Why don’t you just create a limit?
Example:



if(SERVER)then

local Spawned = {}
local dumbo = 0

timer.Create("NPCSpawner", 1000, 0, function()
	for class, val in pairs(NPC.Conf)do
		for _, v in pairs(val.pos)do
			local npc = ents.Create(class)
			npc:SetPos(v.pos)
			npc:SetAngles(v.angle)
			npc:Spawn()
			npc:Activate()

			dumbo = dumbo + 1

			val.cur = {}

			if(!table.HasValue(val.cur, npc))then
				table.insert(val.cur, npc)
				print(table.Count(val.cur).."/"..val.limit.." - "..class)
			end
			if!(IsValid(npc))then
				npc:Remove()
				table.RemoveByValue(val.cur, npc)
			end
			if(table.Count(val.cur) > val.limit)then
				npc:Remove()
				table.RemoveByValue(val.cur, npc)
			end
		end
	end
end)

if dumbo => 25 then
	timer.Remove("NPCSpawner")
end


Untested but should work.

I need to create a limit for each NPC
so that at each spawn of the NCP this limit is not exceeded( NPCs do not exceed in the table and no longer spawning)

You might need to add an onkilled thing to it to take away one dumbo.

LIMIT for a specific class NPC



	npc_mutant_pseudogiant = {
		pos = {

			[1] = {
				pos = Vector(-183.32, -1266.97, -639.97), -- -252.307831 -1332.139282 -575.968750;setang 2.368048 -170.098373 0.000000
				angle = Angle(0, -179.21, 0),
			}

		},
		limit = 1 --[[LIMIT for a specific NPS]]--
	},


LIMIT FOR ALL NPCS



for class, val in pairs(NPC.Conf)do
		for _, v in pairs(val.pos)do
			local npc = ents.Create(class)
			npc:SetPos(v.pos)
			npc:SetAngles(v.angle)
			npc:Spawn()
			npc:Activate()

			dumbo = dumbo + 1 --[[LIMIT for all]]--


I need LIMIT for a specific class NPC in the table

Well, your code with limit wont work as you’re not doing anything with it. All you’re doing is saying limit = 4, then it changes when the next npc comes and sets it to limit = 3.

How did this take 5 days?

[lua]if(SERVER)then

timer.Create(“NPCSpawner”, 1000, 0, function()
for cls,tbl in next,NPC.Conf do
local num = #ents.FindByClass( cls )
if( num >= tbl.limit ) then continue end
for _,v in next,val.pos do
local npc = ents.Create( cls )
npc:SetPos( v.pos )
npc:SetAngles( v.angle )
npc:Spawn()
npc:Activate()
num=num+1
if( num >= tbl.limit ) then break end
end
end
end)

end[/lua]

Inefficient and a bit hacky to do that, but something tells me that won’t be a problem for this darkrp server.

You can probably keep count with an OnEntityCreated and EntityRemoved hook instead of counting each spawn iteration. Also, you should just use pairs instead of manually writing out next and the table – I believe LuaJIT will sometimes return something besides next with pairs_aux, but I may be thinking of ipairs. Either way, much easier to read for little gain (a C-method call).