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
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
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.
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
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