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

Spawning waves of zombies, but function keeps repeating Wave 1?

I'm trying to work out a Zombie wave script, however, the function keeps repeating only wave one. I'm trying to make it so that once all Zombies are killed, it moves onto the next wave. I only about a week into learning Lua so I'm a huge newbie.

local tZombie = game:GetService("ServerStorage").Zombie1
local zCnt = 0
local rnd = Random.new()

print("Spawning Wave 1")

local function spawnZombies()
	for i= -1118, 8, 230 do 
		local zombie = tZombie:Clone()
		zombie.Parent = workspace.Zombies
		local xLoc = rnd:NextInteger(-75,10) 
		local zLoc = rnd:NextInteger(-75,10)
		local pos = Vector3.new(xLoc, 3, zLoc)
		zombie:MoveTo(pos)
	end
end

local function getZCount()
	local num = #workspace.Zombies:GetChildren() 
	return num
end

while true do
	
	if zCnt <20 then 
		print("Spawning Zombies Wave 1")
		spawnZombies()
	end
	zCnt = getZCount()
	wait(5)
end

print("Spawning Wave 2")

local function spawnZombies()
	for i= -1118, 8, 230 do 
		local zombie = tZombie:Clone()
		zombie.Parent = workspace.Zombies
		local xLoc = rnd:NextInteger(-75,10) 
		local zLoc = rnd:NextInteger(-75,10)
		local pos = Vector3.new(xLoc, 3, zLoc)
		zombie:MoveTo(pos)
	end
end

local function getZCount()
	local num = #workspace.Zombies:GetChildren() 
	return num
end

while true do

	if zCnt <30 then 
		print("Spawning Zombies Wave 2")
		spawnZombies()
	end
	zCnt = getZCount()
	wait(5)
end

print("Spawning Wave 3")

local function spawnZombies()
	for i= -1118, 8, 230 do 
		local zombie = tZombie:Clone()
		zombie.Parent = workspace.Zombies
		local xLoc = rnd:NextInteger(-75,10) 
		local zLoc = rnd:NextInteger(-75,10)
		local pos = Vector3.new(xLoc, 3, zLoc)
		zombie:MoveTo(pos)
	end
end

local function getZCount()
	local num = #workspace.Zombies:GetChildren() 
	return num
end

while true do

	if zCnt <40 then 
		print("Spawning Zombies Wave 3")
		spawnZombies()
	end
	zCnt = getZCount()
	wait(5)
end

1 comment

0

Try using a repeat loop on spawning the zombies

1 answer

✓ Accepted answer

--Its quite simple to do what you want, but there are a few things you need to learn as well. Please read all my --'s and learn from them.
local tZombie = game:GetService("ServerStorage").Zombie1
local zCnt = 0
local rnd = Random.new()
local wave = 1 -- We'll use this to determine what wave we are on

local function spawnZombies() -- A function is something that will run the code inside the function whenever you call it. There is no reason to have multiple instances of the same function that do the same thing. Simply define the function once, and anytime you need to use it call it.
	for i= -1118, 8, 230 do  --Why are you setting this up like this? i= # means what value i starts at. The second number is what it increases by, and the third is what it stops at. You want 168 zombies to spawn?    i = 1, 1, 168
		local zombie = tZombie:Clone()
		zombie.Parent = workspace.Zombies
		local xLoc = rnd:NextInteger(-75,10) 
		local zLoc = rnd:NextInteger(-75,10)
		local pos = Vector3.new(xLoc, 3, zLoc)
		zombie:MoveTo(pos)
	end
end

local function getZCount()
	local num = #workspace.Zombies:GetChildren() 
	return num
end

-- Like I said, you only need one function to do everything. If you want values to change you still don't need another function, just add arguements to it like the following example:
--[[
Example function

local function spawnZombies(zombiestospawn, xLoc1,xLoc2,zLoc1,zLoc2)
	for i= 1, 1, zombiestospawn do
		local zombie = tZombie:Clone()
		zombie.Parent = workspace.Zombies
		local xLoc = rnd:NextInteger(xLoc1,xLoc2) 
		local zLoc = rnd:NextInteger(zLoc1,zLoc2)
		local pos = Vector3.new(xLoc, 3, zLoc)
		zombie:MoveTo(pos)
	end
end
]]
workspace.Zombie.ChildRemoved:Connect(function() -- Instead of running a loop to check how many zombies are left, just check to see when one was removed from the zombie storage folder.
	zCnt = getZCount()
	if zCnt < 20 then -- If zombies count is less than 20, start next wave
		wave = wave + 1
		print("Spawning Zombies Wave ".. wave)
		spawnZombies()
	end
end)

1 comment

0

Thanks WizyTheNinja, sorry for the late response, I got distracted finishing the models and GFX for the game. This is extremely helpful, I honestly appreciate it.

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