How would I use parallel lua here?

Hello,

So I have NPCs that choose an action to do based on information it collects.

These are fighting NPCs so they have to react to stuff rather quickly (like to block an incoming attack), so I have it update and check every 0.1 seconds its surroundings (nearby enemies, if there are health drops nearby, how much stamina the NPC has, etc).

Then using that information it makes a decision.

My setup is all of this is in module scripts, and then I have 1 server script which initializes all the NPCs.

This is fine for a few NPCs, but if I have 25+ it starts to lag and slow down. Can I take advantage of parallel lua for this? If so how? I’m kinda looking for some sort of structure / how would I place the actors, etc

Thanks!

1 Like

You can actually take advantage of Parallel Luau, however you may need to rethink some backend behind said npcs due to its nature.

Basically what you would need to do is instead of having a module script for each npc, you would need to have instead an Actor instance along with a Script inside said actor. The script itself will be the one to handle all of the NPC code. However do also keep in mind the fact that IF you had any sort of data that needed to be shared across NPCs then using Parallel Luau might force you into applying some less than desirable methods of sharing data (Firing bindable events back and fourth primarily). Along with the fact that Parallel Luau itself may or may not be 100% reliable as roblox may or may not switch around what and what cant be used in parallel code but im not entirely sure about this.

Still, you will need to make minimal to potentially many changes and additions to your code in order to make it effective and efficient when running in parallel. Sadly it’s beyond easy to create bottlenecks when trying to create Parallel Luau code.