My unfinished Un optimized wave system framework

local roundtime = 30


local unitsperound = {
	Wave1 = 5,
	Wave2 = 15,
	Wave3 = 50,
	Wave4 = 150
}

local Wave = 0

local enemiesLeft = 0

local isinround = false







local enemiesfolder = game.Workspace.Enemies:GetChildren()


function EndRound()
	isinround = false
	game.Workspace.Enemies:ClearAllChildren()
end

function BeginRound()
	Wave = Wave + 1
	isinround = true
	if Wave == 1 then 
		for I = 1, 5 do
			Spawnunit()
		end
	elseif Wave == 2 then
		for I = 1, 15 do
			Spawnunit()
		end
	elseif Wave == 3 then
		for I = 1, 50 do
			Spawnunit()
		end
	elseif Wave == 4 then
		for I = 1, 105 do
			Spawnunit()
		end
	end
end

function Spawnunit()
	local cloned = game.ReplicatedStorage.NPC:Clone()
	cloned.Parent = game.Workspace.Enemies
end

function lose()
	-- 
	wait(2)
	script:Destroy()
	print("lost") -- just realised this should be one line above
end

function win()
	--
	wait(2)
	script:Destroy()
	print("won")
end

function ResetWave()
	enemiesLeft = 0
end

function Getenemies()
	for i, v in pairs(enemiesfolder) do
		enemiesLeft = enemiesLeft + i
		print(i)
	end
end



local RemotEvents = game.ReplicatedStorage.RemoteEvents
local Begingameevent = RemotEvents.Begin
local loseevent = RemotEvents.Lose
local wineevent = RemotEvents.Win
local spawnunitevent = RemotEvents.SpawnUnit
local startroundevent = RemotEvents.StartRound
local endroundevent = RemotEvents.End
local updevent = RemotEvents.UPD 

local Firstroundbegan = false 
-- makeing the actual game part
Begingameevent.OnServerEvent:Connect(function()
	BeginRound()
	startroundevent:FireAllClients(Wave)
	Firstroundbegan = true
end)

local intermissiontime = 5


wait(5)
Firstroundbegan = true

repeat
	print("Repeating")
if Firstroundbegan == true then
		print("Roundtimer Has began")
		BeginRound()
		startroundevent:FireAllClients(Wave)
		wait(roundtime)
		for i, V in pairs(enemiesfolder) do
			if i == 0 then
				ResetWave()
				EndRound()
				endroundevent:FireAllClients(Wave, "Round Has Completed")
			else
				lose()
				loseevent:FireAllClients("Game Lost")
				print("game lost")
			end
		end
	end
until

Firstroundbegan == true


Yes i know i created an array and didnt use it and i have functions im not using and it doesnt really work.
its been changed a lot. it had a lot of For loops before i changed it a bit and had a function called RequestGetEnemies(). I also know that Else if arguments are not very advanced.

1 Like

how are we supposed to give you feedback if there’s no video to showcase it? unless you want us to just review your code

1 Like

I’ll add a video later, I was going to ask for feedback but I think I posted this in the wrong category

Creations feedback is fine. I get it now, your code is about spawning enemies per wave. Instead of using for I, multiple times when spawning units you should place a variable inside unit like this:

function Spawnunit(Amount)

then inside the function you can add For i function:

for i = 1, Amount do
    -- clone
end

Instead of using wait() you should be using task.wait() since it is better and faster.

For the neatness of the code I would recommend placing all variables at the very top of the script.

1 Like

Thank you! I will move the variables to the top and I will replace some of the for loops.
I just googled Wait and I wasn’t aware it was deprecated.

1 Like