Skip to content

getscriptfromthread

Notes on getscriptfromthread

This function should return the script associated with the thread, regardless of any mutations made to itself or its environment.

This also includes threads whose script global variable is set to nil or reassigned - i.e. getscriptfromthread should still return said script. getscriptfromthread should use the script reference stored on the thread itself, not the thread environment's script value. This reference is weak, so it should not keep the script alive.

Returns nil when no script is associated

If the given thread has no associated Script, LocalScript, or ModuleScript, this function should return nil.

This includes executor-created threads. If the thread's weak script reference no longer points to a live script, such as after the script is garbage collected, this function should also return nil.

getscriptfromthread returns the Script, LocalScript, or ModuleScript associated with a given Luau thread.

function getscriptfromthread(thread: thread): BaseScript | ModuleScript | nil

Parameters

Parameter Description
thread The Luau thread whose associated script should be retrieved.

Example

Resolving the script associated with a calling thread
local old; old = hookmetamethod(game, "__index", function(self, key)
    if not checkcaller() then
        local current_thread = coroutine.running()
        local thread_script = getscriptfromthread(current_thread)

        warn("__index access from:", thread_script and thread_script:GetFullName() or "Unknown")

        hookmetamethod(game, "__index", old) -- restore the original
        return old(self, key)
    end

    return old(self, key)
end)

print(getscriptfromthread(coroutine.running())) -- output: nil, because this is an executor thread