Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

help with coroutines?

Asked by 9 years ago

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

Log in to vote
1
Answered by 9 years ago

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)
0
Please accept if this works. Customality 21 — 9y
0
just out of curiosity how would i handle different wait times with different units? ProfessorSev 220 — 9y
0
There, I updated my post. Customality 21 — 9y
0
thanks ProfessorSev 220 — 9y
Ad

Answer this question