Need help with my zombie spawner

I made a server script that spawns zombies in random positions I also made an integer value that keeps track of how many zombies left but the problem is at line 20
Line 20 doesn’t run at all for some reason
Here is my code:

game.Players.PlayerAdded:Wait()
local zombieAmount = game.ReplicatedStorage.ZombieAmount
local text = game.ReplicatedStorage.Text
local Wave = 1
local inWave = true
while wait(0.1) do
	local zombie = game.ReplicatedStorage["Drooling Zombie"]:Clone()
	local PosX = math.random(-240, 240)
	local PosY = math.random(-240, 240)
	zombie.PrimaryPart.Position = Vector3.new(PosX, 5, PosY)
	zombie.Parent = workspace
	zombieAmount.Value += 1
	if zombieAmount.Value == 50 then
		break
	end
end

inWave = false
wait(3)
if zombieAmount.Value == 0 and inWave == false then
	for i = 30, 0, -1 do
	    Wave += 1
		text.Value = "Wave "..Wave.." Is starting in "..i
		wait(1)
	end
end

inWave = true

The if condition will only run 3 seconds after the zombies finish spawning, which is not enough time to set the counter to 0. Run a loop that yields indefinitely until the zombieAmount is 0

did you try using print() on the inWave and zombieAmount values? if so what did they say

And how can run a loop with infinite yield?

I did not do that i will go do it now

while zombieAmount.Value > 0 do 
    wait() 
end

And what do I do after that?
Where do I put this while loop?

They Printed i putted a print() after the wait(3) and it printed false after 3 seconds

inWave = false
wait(3)
print(inWave)
if zombieAmount.Value == 0 and inWave == false then
	for i = 30, 0, -1 do
	    Wave += 1
		text.Value = "Wave "..Wave.." Is starting in "..i
		wait(1)
	end
end

then the problem is indeed the zombieAmount. Did you also print that?

Me telling you exactly what to do isn’t going to help you learn when you have this same problem in the future.

The script will incrementally run from start to finish, and won’t go over it again unless you put it in a loop. In your situation, your if condition is not in a loop and is instead being checked once and once only after 3 seconds. This means that all of the zombies must be killed in 3 seconds, else the condition will run, see zombies are alive, and then ignore the block of code and continue to the end of the script. By putting a condition before the if statement to repeat wait() until zombieAmount.Value > 0, it will keep repeating the code (wait()) until the condition is met zombieAmount.Value > 0, therefore fixing the problem of having it only check once.

2 Likes

Yes it printed the zombie amount I even looked at the value in the properties tab while playing and it decreased as it should

I don’t understand? what do I do at this point?

repeat a block of code so that it doesn’t only check once.

yield the code until the zombieAmount is 0

I don’t understand how can I do that?

for example:

while zombieValue > 0 do
   wait()
end

instead of
wait(3)

I just explained it, use the while ... do ... end syntax in the way I sent above. This would also prevent you having to wrap the other stuff in an if statement as well

The issue is that it only checks once. make sure it is always checking. Via .Changed or while loops. Either work.

Will this work?:

inWave = false

repeat wait() until zombieAmount.Value == 0

if inWave == false then
	for i = 30, 0, -1 do
	    Wave += 1
		text.Value = "Wave "..Wave.." Is starting in "..i
		wait(1)
	end
end

inWave = true

I think you still need to add an end, and if you still want to check for inWave I would also include that on the check. But judging by this code it seems kind of redundant to have anyways.