Help with setting a spawn limit

Hey! I am currently working on a script for a zombie game. The script is simple, however, I am no expert in scripting, therefore I cannot understand how I possibly can make it so the NPC’s that are spawning stop after there is a certain amount of them currently alive.


In the short term, I am trying to prevent NPC’s from spawning when too many of them exist. I am trying to make a limit for entities allowed to exist.
The code is down below, any help is appreciated:

local NPC = game.ReplicatedStorage.Regular
local spawner = script.Parent

while true do
	local Clone = NPC: Clone()
	Clone.Parent = workspace
	Clone.Torso.CFrame = spawner.CFrame
	wait(10)
end

1 Like

Try dedicating a folder in workspace for just those NPCs. Folder:GetChildren() should return only each and one of those cloned NPCs. Use # operator for tables to count and set a limit.

Create a Folder to Keep track of the NPC’s
As @Operatik Stated, Use Folder:GetChildren() and use # to Count them

local MaxEntityCount = 10 -- Max

local NPC = game.ReplicatedStorage.Regular
local Folder = workspace.NPCFolder -- Folder
local Folder_Children = Folder:GetChildren() -- Gets the Children
local spawner = script.Parent

function SpawnNPC()
	local Clone = NPC:Clone()
	Clone.Parent = Folder -- Parents to Folder
	Clone.Torso.CFrame = spawner.CFrame
end

while #Folder_Children < MaxEntityCount do -- Checks if Number of Children is less than Max
SpawnNPC()
task.wait(10)
end

there are many options:

while -- your maximum amount to stop -- do
local Clone = NPC: Clone()
Clone.Parent = workspace
Clone.Torso.CFrame = spawner.CFrame
wait(10)

the other is with repeat until
but for loop don’t use it for that
and of course use the number of tables Folder:GetChildren() and # to count them and set limit

Note this script will stop working and not spawn any more NPCs if they get killed and removed in action. There are two ways of doing it, either infinitely checking, or use an event to resume spawning.

Im pretty sure a while loop Fires again when the Condition is met again

It doesn’t. It exits and then proceeds after the block.

Ah, youre right, Couldnt you do this?

while task.wait(10) do
  if #Folder_Children < MaxEntityCount then
    SpawnNPC()
  end
end
1 Like

So it would look like this?

local MaxEntityCount = 10 -- Max

local NPC = game.ReplicatedStorage.Regular
local Folder = workspace.NPCFolder -- Folder
local Folder_Children = Folder:GetChildren() -- Gets the Children
local spawner = script.Parent

function SpawnNPC()
	local Clone = NPC:Clone()
	Clone.Parent = Folder -- Parents to Folder
	Clone.Torso.CFrame = spawner.CFrame
end

while #Folder_Children < MaxEntityCount do -- Checks if Number of Children is less than Max
	SpawnNPC()
	while task.wait(10) do
		if #Folder_Children < MaxEntityCount then
			SpawnNPC()
		end
	end
end
local MaxEntityCount = 10 -- Max

local NPC = game.ReplicatedStorage.Regular
local Folder = workspace.NPCFolder -- Folder
local Folder_Children = Folder:GetChildren() -- Gets the Children
local spawner = script.Parent

function SpawnNPC()
	local Clone = NPC:Clone()
	Clone.Parent = Folder -- Parents to Folder
	Clone.Torso.CFrame = spawner.CFrame
end

	while task.wait(10) do
		if #Folder_Children < MaxEntityCount then
			SpawnNPC()
		end
	end

Alternative to this is:

while true) do
	if #Folder_Children < MaxEntityCount then
		SpawnNPC()
	end
task.wait(10)
end

I have tried and did the following:

  • Put the script into the spawner part

  • Created a folder within workspace, called it NPCFolder.

  • Inserted the “Regular” NPC model into ReplicatedStorage.

The script seems to partly work. The NPC’s do spawn, but there is still no limit to this, and they just keep spawning.

I think i see my Error, Try this?

while task.wait(10) do
local Folder_Children = Folder:GetChildren()
		if #Folder_Children < MaxEntityCount then
			SpawnNPC()
		end
	end
1 Like

Yes! This is the correct solution. The help is much appreciated!

local MaxEntityCount = 5 -- Max

local NPC = game.ReplicatedStorage.Regular
local Folder = workspace.NPCFolder -- Folder
local Folder_Children = Folder:GetChildren() -- Gets the Children
local spawner = script.Parent

function SpawnNPC()
	local Clone = NPC:Clone()
	Clone.Parent = Folder -- Parents to Folder
	Clone.Torso.CFrame = spawner.CFrame
end

while task.wait(10) do
	local Folder_Children = Folder:GetChildren()
	if #Folder_Children < MaxEntityCount then
		SpawnNPC()
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.