Do ModuleScripts work in actors?

The title says it, I couldn’t find the answer so I’m asking if ModuleScripts work in actors and do the same thing that a Script would if it was in an actor. (Of course, if called on server)
Thanks!

2 Likes

Module scripts can be required and used by actors, but for parallel execution an actor must be a script or a local script that is run automatically.

A module parented to its own actor instance will be loaded as part of the LuaVM of the script that requires it. If it is loaded by a script that is not an actor, that variant of the module won’t run in parallel. If this same module is (also) required by an actor, the other variant will run with that actor.

I should mention that a single VM does not run on multiple CPU cores, and that normally each actor gets its own VM, which shows, for example, when you add a key to the same module in one actor and it doesn’t exist in the module variant of another actor. Still, from time to time, actors might share a LuaVM, thus the same module variant.

Note: ‘Variant’ is just a term I used to differentiate between distinct packages loaded using the same module scripts.

4 Likes

When I tested it, modules parented to actors could call parallel functions, but serial scripts requiring them could not use their parallel functions.

image

image

image

3 Likes

I appreciate you bringing this observation up. What’s intriguing about this is that even modules can use functions and callbacks to enter parallel phase as long as their parent is an actor.

I made another quick test to confirm that they do, but they don’t cross over to another thread and they’re still resumed one after another on the main worker.

Details

image

Script:

local actorFolder = game.ServerScriptService.Actors
local actors = actorFolder:GetChildren()

for i = 1, #actors do
    local module = require(actorFolder["Actor"..i]:FindFirstChildOfClass("ModuleScript"))  
end

while true do
    for _, actor in actors do
        actor:SendMessage("RunInParallel")
    end
    task.wait()
end

Module scripts:

script:GetActor():BindToMessageParallel("RunInParallel", function()
    for i = 1, 1e5 do
        local x = i^2
    end
end)
return {}

And here’s when a script in Actor1 requires ModuleScript1 in Actor1 and ModuleScript2 in Actor2, and another script in Actor3 requires ModuleScript3 in Actor3 and ModuleScript4 in Actor4.

4 Likes

How does the microprofiler work? How do I see if a thread is parallel or serial? I assume it has something to do with the modulescript threads being under a bar named “runParallel”.

1 Like