[SOLVED] Issue running multiple functions at once

Hello Developers!
I am currently running into a problem with my tower defense wave system; its not game-breaking, but it could make the sort of gameplay im looking for impossible.

So my problem is that I want to make stuff spawn, without waiting for the previous sequence to finish spawning. Iv’e tried using coroutine.wrap, but the results were not what I was looking for.

How can I solve my problem? Thanks!

Here is the spawn script:

function Spawn_Sequence(wave)
	for i, sequence in pairs(wave) do
		task.wait(sequence.SpawnDelay)
		for x=1, sequence.EnemyCount do
			coroutine.wrap(addToDatabase)(sequence.Enemy)

			local model = EnemyModels[sequence.Enemy.Name]:Clone()
			local EnemyID = #Database
			local randomOffset = Random.new():NextNumber(-1, 1)

			model.Name = tostring(EnemyID)
			model:SetAttribute("EnemyName", sequence.Enemy.Name)
			model:SetAttribute("PathOffset", randomOffset)
			model.Parent = workspace.Enemies
			model:PivotTo(workspace.Path.StartNode.CFrame * CFrame.new(randomOffset, sequence.Enemy.HipHeight, 0))

			coroutine.wrap(Move_Enemy)(sequence.Enemy, EnemyID)
			coroutine.wrap(SetupEnemy)(sequence.Enemy, EnemyID)

			task.wait(sequence.SpawnRate)
		end
	end
end

And here is the loop that handles spawning sequences:

local function Wave_System()
	for count, wave in pairs(Waves.Casual) do
		coroutine.wrap(Spawn_Sequence)(wave.Enemies)
	end
end
2 Likes

I’m not sure why you’re using coroutines for the wave system, that will make every wave play at once. I would also recommend using task.spawn. It’s more lightweight and better fits your purposes. You should also be setting .Parent last for these reasons.

Spawn code:

function Spawn_Sequence(wave)
	for i, sequence in pairs(wave) do
		task.wait(sequence.SpawnDelay)
		
		for x=1, sequence.EnemyCount do
			task.spawn(addToDatabase, sequence.Enemy)

			local EnemyID = #Database
			local randomOffset = Random.new():NextNumber(-1, 1)

			local model = EnemyModels[sequence.Enemy.Name]:Clone()
			model.Name = tostring(EnemyID)
			model:SetAttribute("EnemyName", sequence.Enemy.Name)
			model:SetAttribute("PathOffset", randomOffset)
			model:PivotTo(workspace.Path.StartNode.CFrame * CFrame.new(randomOffset, sequence.Enemy.HipHeight, 0))
			model.Parent = workspace.Enemies
			
			task.spawn(Move_Enemy, sequence.Enemy, EnemyID)
			task.spawn(SetupEnemy, sequence.Enemy, EnemyID)

			task.wait(sequence.SpawnRate)
		end
	end
end

Loop code:

local function Wave_System()
	for count, wave in pairs(Waves.Casual) do
		Spawn_Sequence(wave.Enemies)
	end
end

i had no idea task.spawn existed, thanks! Im gonna try out the code later.

using task.spawn worked, but it is still having the same issue. I want, for exemple, two kinds of Zombies to spawn at the same time. The code I have right now makes it so that it spawns a sequence of enemies, and then it waits for the full sequence to have been spawned to spawn the other sequence of enemies. I dont wait this wait right there.

Ohhhh, just wrap task.spawn around the other loop then. Get rid of the other task.spawns if you don’t need them.

Code:

function Spawn_Sequence(wave)
	for i, sequence in pairs(wave) do
		task.wait(sequence.SpawnDelay)
		
		task.spawn(function()
			for x=1, sequence.EnemyCount do
				task.spawn(addToDatabase, sequence.Enemy)

				local EnemyID = #Database
				local randomOffset = Random.new():NextNumber(-1, 1)

				local model = EnemyModels[sequence.Enemy.Name]:Clone()
				model.Name = tostring(EnemyID)
				model:SetAttribute("EnemyName", sequence.Enemy.Name)
				model:SetAttribute("PathOffset", randomOffset)
				model:PivotTo(workspace.Path.StartNode.CFrame * CFrame.new(randomOffset, sequence.Enemy.HipHeight, 0))
				model.Parent = workspace.Enemies

				task.spawn(Move_Enemy, sequence.Enemy, EnemyID)
				task.spawn(SetupEnemy, sequence.Enemy, EnemyID)

				task.wait(sequence.SpawnRate)
			end
		end)
	end
end

Make sure you’re using the loop code I sent.

ahhh, thanks, that worked out. Thank you! and thank you again for showing to me task.spawn. I had no idea it existed.

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