Continue wave when enemies are killed

Im trying to make an tower defense game but I’ve ran into the problem that the next wave does not begin when all enemies are killed

This the script:

if script.Parent.Friendly.Health.Value >= 0 and currentwave == 0 then
	currentwave = currentwave + 1

	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	enemiesspanwed = true
	
	while enemiesspanwed == true do
		wait(.1)
		minionfolder.ChildRemoved:Connect(function()
			if #minionfolder:GetChildren() == 0 then
				print("wave completed")
				currentwave = currentwave + 1
				wavecompleted = true
			end
			enemiesspanwed = false
			if currentwave ~= 2 then
				currentwave = 2
			end
			print(currentwave)
		end)
	end	
end
	
	

if script.Parent.Friendly.Health.Value >= 0 and currentwave == 2 and wavecompleted == true then
	
	wavecompleted = false
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()
	wait(spawnTime)
	spawnNormal()

It removes the child from the minionfolder when it dies and sets the currentwave to 2 but it still does not activate the 2nd round

First of all, your code is unoptimized and not ideal, its basically yandere dev code, which is pretty BAD. Executing a high number of functions in the same script can really impact the game’s performance. You should fix it.

okay how would I fix it then… and that doesn’t really awnser my question.

First of all, you shouldn’t use an if statement to control what wave is being executed. You can easily use a number value and add 1 to it every time the enemies in a wave get cleared. This is just one of the things you need to fix.

nevermind I found a video on it so I’ll work on that later on, thanks for letting me know though

use a pairs loop or a while wait() loop instead of constantly repeating the same code.

Quick advice, you should not jump into making large projects if you are a beginner. Making small projects while you learn is an excellent way of learning luau.

minionfolder.ChildRemoved:Connect(function()
	if #minionfolder:GetChildren() == 0 then
		print("wave completed")
		currentwave += 1
		wavecompleted = true
	end
	if currentwave ~= 2 then
		currentwave = 2
	end
	print(currentwave)
end)

if script.Parent.Friendly.Health.Value >= 0 and currentwave == 0 then
	currentwave += 1	
	for i = 1, 5 do
		wait(spawnTime)
		spawnNormal()
	end
end

if script.Parent.Friendly.Health.Value >= 0 and currentwave == 2 and wavecompleted == true then
	wavecompleted = false
	for i = 1, 10 do
		wait(spawnTime)
		spawnNormal()
	end
end

This won’t work but I did some cleaning up for you.