Why are these tables kind of 'replacing' eachother?

so i’ve been trying to organize a way to spawn enemies through tables on a modulescript, and, to make my life easier, i also created this function that can repeat through a bunch of enemies at once, throw them into a table, and then i can just unpack them to add 40 in 1 line of code. however, when i have multiple of these functions in 1 wave, the ones after replace the ones before them.
how can i make both functions run without overlapping and covering their predecessors

MODULESCRIPT:


function Repeat(Enemy,amount,Table)
	local repeats = 0
	repeat
		table.insert(Table,Enemy)
		repeats += 1
	until
	repeats == amount
	return Table
end

local Waves = {
	["Wave 1"] = {
		table.unpack(Repeat(game.ServerStorage.Noob,40,{})),
	},
	["Wave 2"] = {
		table.unpack(Repeat(game.ServerStorage.Noob,20,{})),
		table.unpack(Repeat(game.ServerStorage.Sworder,20,{})),
	},
	["Wave 3"] = {
		table.unpack(Repeat(game.ServerStorage.Noob,50,{})),
		table.unpack(Repeat(game.ServerStorage.Sworder,30,{})),
	},
	["Wave 4"] = {
		table.unpack(Repeat(game.ServerStorage.Booster,10,{})),
		table.unpack(Repeat(game.ServerStorage.Noob,30,{})),
		table.unpack(Repeat(game.ServerStorage.Sworder,15,{})),
	},
	["Wave 5"] = {
		table.unpack(Repeat(game.ServerStorage.Tanker,5,{})),
		table.unpack(Repeat(game.ServerStorage.Noob,30,{})),
	},
}


return Waves

printed message of wave 2:
image
(1 noob then 20 sworders)

3 Likes

You might find the solution to your problem with the use of the function task.spawn. It creates a separate thread meaning you can run multiple functions at once.

what do you mean by this, and how would i even integrate this?

You are better off using a for loop to merge multiple tables

Here’s the explanation why unpack won’t work:

Here’s one solution I found:

You can organize enemy spawns without using all this table magic. Set a “Count” variable that tells you how many of a specific enemy type to spawn.

local Waves = {
	["Wave 1"] = {
		{
			Type = "Noob",
			Count = 40,
		}
	},
	["Wave 2"] = {
		{
			Type = "Noob",
			Count = 40,
		},
		
		{
			Type = "Sworder",
			Count = 40,
		},
	},
}

Then, when you’re spawning the enemies:

-- This is for spawning enemies for one wave
-- So waveStructure should be Waves["Wave 2"], for example
for i, enemySpawnBlock in waveStructure do
	local enemyType = enemySpawnBlock.Type
	
	local enemyModel = ServerStorage[enemyType]
	
	for j = 1, enemySpawnBlock.Count do
        -- However you spawn enemies here
		Spawn(...)
	end
end
2 Likes

why is nobody optimizing this?

local Waves = {
	["Wave 1"] = {
		table.create(40, game.ServerStorage.Noob),
	},
	["Wave 2"] = {
		table.create(20, game.ServerStorage.Noob),
		table.create(20, game.ServerStorage.Sworder),
	},
	["Wave 3"] = {
		table.create(50, game.ServerStorage.Noob),
		table.create(30, game.ServerStorage.Sworder),
	},
	["Wave 4"] = {
		table.create(10, game.ServerStorage.Booster),
		table.create(30, game.ServerStorage.Noob),
		table.create(15, game.ServerStorage.Sworder),
	},
	["Wave 5"] = {
		table.create(5, game.ServerStorage.Tanker),
		table.create(30, game.ServerStorage.Noob),
	},
}


return Waves

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.