so i got this script that controls the main spawning for zombies
local CS = game:getService("CollectionService") while true do wait(5) for i, v in pairs(CS:GetTagged("spawner")) do if game.ReplicatedStorage.Values.gameInProgress.Value == true then if game.ReplicatedStorage.Values.zombiesRemaining.Value > 0 then local NPC = game.ReplicatedStorage.Zombie:Clone() NPC.Parent = v NPC.HumanoidRootPart.CFrame = v.CFrame CS:AddTag(NPC, "Zombie") game.ReplicatedStorage.Values.zombiesRemaining.Value = game.ReplicatedStorage.Values.zombiesRemaining.Value - 1 end end end end
but i want to make a cap of like 24 - 30 zombies allowed at a time. when the rounds get higher too many zombies spawn into the map. but i dont know how to write it out. an idea i had was this
local CS = game:getService("CollectionService") while true do wait(5) for i, v in pairs(CS:GetTagged("spawner")) do if game.ReplicatedStorage.Values.gameInProgress.Value == true then if game.ReplicatedStorage.Values.zombiesRemaining.Value > 0 then local NPC = game.ReplicatedStorage.Zombie:Clone() NPC.Parent = v NPC.HumanoidRootPart.CFrame = v.CFrame CS:AddTag(NPC, "Zombie") game.ReplicatedStorage.Values.zombiesRemaining.Value = game.ReplicatedStorage.Values.zombiesRemaining.Value - 1 end if game.ReplicatedStorage.Values.gameInProgress.Value == true then if game.ReplicatedStorage.Values.zombiesRemaining.Value < 24 then end end end end end
but i honestly dont know how to write out "stop spawning" .
So, I found a few flaws in your code.
In first like, make 'g' capital.
local CS = game:GetService("CollectionService")
Since, your zombie spawning script (script 2), [on Line 6 and Line 14], you are checking the same value from Replicated Storage again and again. So, I prefer you to remove the 'if' statement of Line 14.
if game.ReplicatedStorage.Values.gameInProgress.Value == true then
Also, you said you want to spawn 24-30 zombies. So, at start you can create a local variable with math.random.
local ZombieSpawn = math.random(24, 30) -- 24 is minimum and 30 is maximum
At line 15, instead of:
if game.ReplicatedStorage.Values.zombiesRemaining.Value < 24 then -- You can do: if game.ReplicatedStorage.Values.zombiesRemaining.Value < ZombieSpawn then
If you want to stop the spawning of zombies, you can simply add break after the line:
if game.ReplicatedStorage.Values.zombiesRemaining.Value < ZombieSpawn then break end
Make sure that 'break' will exit the loop. So, I recommend you to add a function or trigger(if/else statements or Remote Events) to trigger the zombie loop again.
Lemme know if it helped!