hi, so im working on a zombie type game. Im trying to work on reducing lag and limiting the amount of zombies on screen. i have three values: zombiesAlive, zombiesRemaining, and zombiesOnScreen
zombiesAlive sets the number of how many zombies will be in a round, for example 6.
zombiesRemaining takes that value (6) pulls it into itself, so now it'll be set to 6 and will begin to decrees as zombies spawn, so that only the number given by zombiesAlive is all that spawns.
zombiesOnScreen gets added values by 1 each time a zombie is spawned in. i want to use this value as the indicator when to stop the loop/stop spawning
here's my script so far
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 game.ReplicatedStorage.Values.zombiesOnScreen.Value = game.ReplicatedStorage.Values.zombiesOnScreen.Value + 1 if game.ReplicatedStorage.Values.zombiesOnScreen.Value == 3 then break end end end end end
I only want 3 zombies on screen, i tried adding in a break to stop the loop
if game.ReplicatedStorage.Values.zombiesOnScreen.Value == 3 then break end
but zombies will still spawn in. what gives?
You made the code more complicated than it needs to be. You'd only really need 3 values in this case;
I wrote up an example, however it still lacks things like removing the zombies when they die etc, which you could probably figure out yourself, tell me if this solves your issue:
local spawners = workspace.Spawners:GetChildren() local values = game:GetService("ReplicatedStorage").GameSettings local remaining = values.zombiesRemaining local inProgress = values.inProgress local zombieLimit = values.zombieLimit inProgress.Value = true zombieLimit.Value = 5 local zombie = values.Zombie:Clone() local i = math.random(1,#spawners) zombie:SetPrimaryPartCFrame(spawners[i].CFrame * CFrame.new(0,3.2,0)) zombie.Parent = workspace remaining.Value += 1 while true do wait(5) if inProgress.Value and remaining.Value > 0 and remaining.Value <= zombieLimit.Value then local zombie = values.Zombie:Clone() local i = math.random(1,#spawners) zombie:SetPrimaryPartCFrame(spawners[i].CFrame * CFrame.new(0,3.2,0)) zombie.Parent = workspace remaining.Value += 1 end end
Try putting the
if game.ReplicatedStorage.Values.zombiesOnScreen.Value >= 3 then break end
before the end
on line 19