Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Loop not breaking? how to stop spawning of zombies

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?

2 comments

0

Try printing before you break

0

i added print, it printed in output, but still did not break the loop :L

2 answers

You made the code more complicated than it needs to be. You'd only really need 3 values in this case;

  • How many zombies is the limit?
  • Is the game running?
  • How many zombies do we have remaining?

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

2 comments

0

"if inProgress.Value and remaining.Value > 0" inprogress.Value is going to end with "attempt to index number with 'Value'", so what are you trying to do here? im adjusting code to match my game as i type this but im trying to understand what you meant here

0

actually wait-- im getting a "attempt to compare number and Instance" error...

Try putting the

if game.ReplicatedStorage.Values.zombiesOnScreen.Value >= 3 then
  	 break
end

before the end on line 19

This discussion is preserved as it was. New answers, comments, and votes are closed.