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.
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
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.
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
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
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