How to know if a module was required in a vm or an actor?

I have a module that I want it to behave differently inside a vm or an actor, but I don’t have any idea on how to detect if that’s the case or not, and I couldn’t find anything about it and there was no information on it on the Parallel Luau article either.

Now a workaround that does work is trying to connect to an even with :ConnectParallel() inside a pcall() and seeing if it errors but I’d prefer it if there is an simple function that I could call to detect it and I wanted to know if anyone knows anything about it.

Thank you for your time.

2 Likes

I think of logic systems check likesome:

shared-table (with using actors sending a message, i meant)

where script is parented to

(edited)

I’m not exactly following?
What is “shared-table (parallel)”?

I changed previous message (meaning is there)

i think of Second method

the other method is by adding logic of:

table, a script that runs is creating what is running (serial or parellel) is saved in modulescript or sharedtable (which is opened for every script)

add check of this Modulescript[Script.Name] (dictionary’s value) is equal to serial then process or if parellel then process different one from serial

if script (dictionary’s key is name of it) is running on parellel then script changes the modulescript (dictionary’s value to “Parallel”)

otherwords add you own system of scripts of running in… (like serial or parallel) then get it and then make logic system and then so on…

cant you have like module.init(script) and in module check if script.Parent is an Actor (script being the argument passed in init

It just seems like the work around I found is much easier and faster than all that

I really don’t want to do that if possible, the things in that module has to run as soon as it’s required and I don’t want to wait until the init function is called, but some of the stuff that the module do as soon as it’s required, I don’t want it to do inside vms

???

local function init(script)
    if script.Parent:IsA("Actor") then

    end
end

return init
require(path)(script)

Again, I don’t want to do that, and also isn’t the work around I found easier and more simple than this?
If there’s no way just call a function to check if the module is running in a VM or not then that’s fine I’ll just use my work around, I don’t want to do these stuff that makes the way to require this module annoying and harder to understand

Misusing pcall is not a “workaround” if you already have a proper way of checking this.

If you still prefer using pcall, then at least do something like this

local success, err = pcall(function()
	task.desynchronize()
	task.synchronize()
end)

if success then
	print("Likely running in parallel context")
else
	print("Not in parallel context")
end

init is stll the best practice, which you should go for.

Okay two questions.

  1. how is this a “Misuse”?
  2. task.desynchornize doesn’t work (I tried that before), and even if it did, why is that any better than connectparallel?

because you are not using it for the things it’s intended for

isn’t much a difference, it’s just what i’d do.

you are still off better using init function, and stop whining about makes the way to require this module annoying, thats what development is

If the module has any functions you can pass a bool, true would mean you are in parallel mode and false means you wouldn’t, and your functions could do something depending on that value

quit being so aggressive. what does an impulsive response give to both of you?

@Sir_Pixelated, you could just do

local success, actor = pcall(script.GetActor, script)

if success then
    if actor then
        print("i am in an actor!")
    else
        print("i am not in an actor :(")
    end
else
    warn("i can't get the actor:", actor)
end

reason is instead of passing an anonymous function, you can just protected call script.GetActor and pass in the module (script) as self, which is justscript:GetActor(). this also prevents your module from straight crashing if there isn’t a result.

Thanks for the kind response!
But unfortunately script:GetActor() is basically the same as script:FindFirstAncestorWhichIsA("Actor"), so using it in a module only returns nil.
So that’s not gonna work either

then you might as well…

local isParallel = pcall(function()
    local event = Instance.new("BindableEvent")
    event.Event:ConnectParallel(function() end)
end)

if isParallel then
    print("i am parallel!")
else
    print("i am serial :(")
end
2 Likes

Yeah that’s the same solution I’ve reached but I just wanted to know if there’s any other that I might have missed

nah, thats pretty much the only way you can do it besides using task.desynchronize()

1 Like

thats literally the least practical way to do it