So I have a basic modulescript, with a print function inside that prints to the output when the modulescript is required. According to the Roblox Documentation, modulescripts only run once, and when called on subsequent times, it just returns the value it already ha returned the first time, resulting in only one of these.
However,
When I require the same module multiple times under different Actor instances, the modulescript will run again, returning another copy of the original one.
Is this intended behavior? If so, how can I make it so I can still have a localscript run under an actor and return the same object when requiring a modulescript?
Example
-- Simple ModuleScript
print ("Requiring the ModuleScript")
local ModuleScript = {}
return ModuleScript
2 Local scripts have code requiring the module
-- Localscript
local Module = require(ModuleScript)
The expected output
Requiring the ModuleScript
The output I get
Requiring the ModuleScript
Requiring the ModuleScript
This is what I mean when it is running more than once, and returning different objects.
It’s important to mention that scripts that are part of the same Actor always execute sequentially with respect to each other. For example an NPC is probably a good candidate to become an Actor. Any parallel-enabled behavior scripts underneath a single NPC Actor will run serially on a single thread, but if you have multiple NPC Actors, each of them will run in parallel on its own thread.
I have two plausible solutions for you, but there might be more:
1- Put every script that you need to require a specific module, under a single Actor. For example, suppose you have ScriptA and ScriptB that you need to share a single module. Put both ScriptA or ScriptB under the same Actor.
2- Use instances that have read-safe properties to manage states instead of modules (check on the docs for properties that are read-parallel and safe). Obviously instances are pretty resource-intensive when compared to a single empty table, but if you use string or number values for example, to manage a state for a module, you can control behaviour and sort of have different behaviours according to what the state is. Only downside is that read-parallel properties cannot be written to.
I have a tool with an actor inside of it containing a localscript that requires a custom Camera Module modifying my camera.
I also have a localscript in starterplayerscripts requiring the Camera Module, but running on the main thread.
How would I make it so that when I require the Camera Module, each localscript references the same returned object?
I can’t use your first solution since I want the localscript under the tool to run on a different thread
I can’t use yout second solution since the instance I am using is the Camera, and I need to write to them.
I don’t think you can, not with normal tables at least. Typically camera controllers aren’t very computationally expensive though, are you sure you need parallel for it? If you need multiple asynchronous functions to run at the same time, maybe you could look into coroutines but do note that they aren’t real threads, they just act as two threads though by breaking what needs to be run, into smaller subroutines. It’s rarely if ever noticeable by humans though.