Implementing Check For Monsters Spawned

Hello, I’m working on a monster spawn system that currently spawns a new monster every 30 seconds. I want to implement a check for if there are more than 5 monsters currently alive no more can spawn.
My current script:

while true do
	local monster = game:GetService("ServerStorage"):WaitForChild("Monster"):Clone()
	monster.Parent = workspace
	monster:MoveTo(game.Workspace.MonsterSpawn.Position)
	wait(30)
end
1 Like

have you considered placing monster models inside a separate folder inside of workspace? you would be able to do something like

local MonsterFolder = instance.new("folder")
MonsterFolder.Parent = workspace
while #workspace.MonsterFolder:getChildren() < 5 do
	local monster = game:GetService("ServerStorage"):WaitForChild("Monster"):Clone()
	monster.Parent = workspace.MonsterFolder
	monster:MoveTo(game.Workspace.MonsterSpawn.Position)
	wait(30)
end
2 Likes

What Alyrian said is what I would do to check the number of zombies. You can implement the check by checking #(—folder here—).

2 Likes

Ok, so the script works, I just have one problem, once all of the monsters are killed no more spawn. How do I clear up the folder once all of the monsters are dead so more can spawn?