Use Parallel Lua with ModuleScript?

I just finished coding my own pathfinding using A*.

It is all nicely in 1 module script and I can call Pathfind:GetRoute(pos1, pos2) and it returns a table of positions to walk to.

It doesn’t rely on any outside services, just accessing nodes (parts in workspace).

How can I take advantage of parallel lua for this? It is mainly just a bunch of distance checks.

The problem with parallel Luau is that there is no direct communication between threads. There are SharedTable objects, but I’m not sure about their performance overhead.

What does your module do exactly? Is it just an interface for PathfindingService or does it implement a custom pathfinder? If it’s using the service, you probably don’t have to do anything. The pathfinding will be done on the thread of the actor that required your module.

If it’s a custom implementation, then get ready to get your hands dirty. The same module required from 2 scripts in separate actors will return 2 separate tables. You can test it by pasting this into a script parented to an actor parented to a module script:
print(tostring(require(script.Parent.Parent)))
Each script will print out a different string.

The way I handle this is with a very simple interface module. The module first checks whether a “LOADED” attribute is true and waits for it to change with GetAttributeChangedSignal("LOADED"):Wait() if not. Theres a script parented to an actor inside this module that does all the logic and this makes sure it’s loaded before any methods are called. The interface module fires/invokes bindable events/functions parented to it.