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
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
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.
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
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.