Coroutine vs several scripts

Hello!
I’m trying to make a game with different parts being managed and I’ve come across an issue.
My game will be like an obby with different sections in it, with each section containing moving parts that will be spawned and destroyed at different times.

Here’s a screenshot for an outline of a section:
image
My question is simple, what’s the best way to handle multiple sections?
Would it be to make multiple scripts for each section? or should I have a master script that creates coroutines for each individual section?

For instance if I go with coroutines and have 3 sections, the master script will create a coroutine for each section which has it’s own while loop that terminates when the game is over (meaning the parts will stop moving and no new parts will spawn in each section).
Im looking at this from an efficiency standpoint, what’s less server-intensive, with my second priority being difficulty of implementation.
Thanks for the feedback :slight_smile:

Go for coroutines since using separated scripts is just messy. Running multiple coroutines at the same time shouldn’t lag your game unless you use way too many of them.

3 Likes

I am pretty sure that’s up to you, both run in multiple threads and result pretty much the same.
Whatever is easier for you to use, do it.

When using multiple scripts, the master script is not too long and hard to read, and everything is divided into a short code that’s simple to read and change.

One advantage when using coroutines and a master script, you can easily make changes, so you don’t have to go through lots of scripts. But that’s only if the script is general enough, and not too specific for each section.

Just keep in mind, not everything needs a new thread. For example:
A kill part, is simply a connection, you don’t have to create a new thread just to create the connection.
You can, but it’s unneseccary

1 Like

I choose coroutines since they have many benefits:

  1. Easy to manage and organize
  2. Can be started, paused, resumed, and even ended whenever you want to.
  3. You don’t need global variables like _G, to share data between threads.
  4. You can create really fast and efficient scripts using them.
1 Like

I would personally use a ModuleScript in as a class to represent a section, in an OOP manner, and have the section objects managed by another module, which would most likely be some kind of singleton that owns the game loop. There are other, equally-valid ways to architect a game, but for someone who comes from a C++/Java app development background, that is what would be natural for me to implement.

2 Likes

Separate scripts and coroutines are basically equivalent performance-wise. Roblox only ever runs exactly one thing at a time—script, coroutine, event handler, whatever.

The best option is probably something like what @EmilyBendsSpace said. Have a certain set of types of things you can have and tag them with the CollectionService | Roblox Creator Documentation. Update everything from one game loop. Also see

Buuuuuuut also, it doesn’t really matter. Just do whatever’s easiest and will make your game actually get made. Refactor later.

1 Like