I have a spawn function that should get all the children inside a folder, get the spawners for zombies and then check if the amount of zombies inside the folder is less than a specific value. If it is lower than xyz it will spawn more until it reaches xyz then the function breaks
local function Handler.SpawnZombies()
local CanSpawnZombies = script.CanSpawnZombies
while true do
for number, ZombieSpawners in pairs(workspace.Map.Model.Spawners:GetChildren()) do
for SpawnedZombies in pairs(workspace.Map.Zombies:GetChildren()) do
if ZombieSpawners.Name == "spawner" then
if CanSpawnZombies.Value == true then
print(SpawnedZombies)
if SpawnedZombies < zombiesAlive.Value or SpawnedZombies == nil then
local zombie = game.ServerStorage.Zombies[currmap.Value][currmap.Value.." Zombie"]:Clone()
zombie.Parent = workspace.Map.Zombies
zombie.CFrame.Position = math.random(#ZombieSpawners.CFrame.Position)
workspace.Map.Zombies.ChildAdded:Connect(function(d) -- if a zombie is added add one
SpawnedZombies += 1
end)
if SpawnedZombies == zombiesAlive.Value then
CanSpawnZombies.Value = false
break
end
end
end
end
end
end
task.wait()
end
end
Here is the tree for more understanding