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

help with coroutines?

so in my game i will have units that can be bought and they are bought through a timer sorta thing where it takes like 10 seconds to make a soldier. so i am going to use coroutines to make the que for them but coroutines are relatively new to me and i am confused on how to use them if i dont know how many that i will need at any given time. so could someone please clarify this for me. thank you.

1 answer

✓ Accepted answer

For this, you would not use coroutines. You would use a table.

local Queue = {"Soldier", "Soldier"}

spawn(function()
	while wait(10) do
		local I = table.remove(Queue, 1)
		--Create your soldier. I is the name of the type or whatever you would do..
	end
end)

Update:

The OP wanted to have different times for different units. Here's how:


local Queue = {"Soldier", "Tank"}

spawn(function()
	while wait() do
		local I = table.remove(Queue, 1)
		if I == "Soldier" then
			wait(10)
			--Create soldier
		elseif I == "Tank" then
			wait(20)
			--Create tank
		end
	end
end)

4 comments

0

Please accept if this works.

0

just out of curiosity how would i handle different wait times with different units?

0

There, I updated my post.

0

thanks

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