Running multiple coroutines. Possible?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    What I want to do is looking more and more like it will need separate threads for it to execute properly, or execute at all, in some instances. Beyond whether multiple coroutines can run around the same time, is it possible to create a coroutine within a coroutine? Would that work in some way?

To give the scenario, there will be multiple units moving around the map, each needing their own coroutine to do so, but along that, while the pathing is happening there is a possibility of running into an enemy unit (second coroutine comes in to play). The ability to have that combat happen and still have multiple units moving around and the possibility of other battles is very important. The only good news I can think of, assuming old info is still true, I can (maybe) yield the coroutine the pathing is running on in order to substitute for the battle, and do this for each pathing coroutine.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Looking through devforums and ROBLOX’s help, however, either answers are old and possibly outdated, or the idea I had of coroutines will not work for what I want to achieve.
1 Like

It probably is possible. Coroutines are used to run a function alongside other lines of code at the same time in the same script. You should probably try it yourself and see if it works.

See, I may be misinterpreting how to use the coroutine properly, yet alone effectively.

There is an entire wiki article on it:
https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine

Yea I’ve looked through that one and another one, but they don’t quite answer my question. I’m doing my own testing right now, see what I can find.

Edit: Looks like what I want to do is possible. Not sure yet with many coroutines running ‘at once’ but definitely a good start for now.

1 Like