New Function task.canDesync() - Check whether a ModuleScript's calling script is a descendant of an Actor!

When developing on Roblox, it is currently difficult to check whether code in a ModuleScript is running in the context of an Actor or not. In other words, I would like to check whether the calling Script is in an Actor or not using a simple function, task.canDesync(). This function returns true if task.desynchronize() and task.synchronize() can be used without throwing warnings; false otherwise.

If this issue is addressed, it would improve my development experience by allowing me to selectively enable/disable multithreading logic based on a simple check of task.canDesync().

For example, let’s say I have the following code in a ModuleScript:

function SampleModule.Test()
    task.desynchronize()
    
    print("Script can multithread!")

    task.synchronize()
end

This code won’t give any warnings when called from a script inside an Actor, but will throw warnings if the calling script is not a descendant of an Actor. The main problem is that sometimes module code needs to be called from both Actor and non-Actor sources, but there’s no simple way for the module to tell whether the calling script is in an Actor.

Instead of the above code, I want to be able to do:

function SampleModule.Test()
    if task.canDesync() then
        task.desynchronize()
    
        print("Script can multithread!")

        task.synchronize()
    else
        print("Script cannot multithread!")
end

At this time, there are alternative solutions to detect whether the calling script is a descendant of an Actor, as discussed in this thread. While these solutions may work for now, they take up a lot of visual space that can be simplified into using a single function, task.canDesync().

6 Likes