Scripts

getscriptthread

Returns the Lua thread associated with a script object when Real can find one.

Syntax

getscriptthread(script: userdata): thread | nil

Arguments

NameTypeDescription
scriptuserdataScript object to look up. Accepted ClassName values are Script, LocalScript, and ModuleScript.

Returns

NameTypeDescription
threadthread | nilAssociated Lua thread, or nil when no matching live thread is found.

Description

Returns the Lua thread associated with a script object when Real can find one. The argument must be object. The function reads the value's ClassName field and raises "not a valid Instance" if that field is not a string. Accepted ClassName values are Script, LocalScript, and ModuleScript; any other value raises "Script expected". It first checks Real's ScriptThreadCache by the script reference. On a cache hit, it returns the cached Luau thread as a thread. On a cache miss, Script values must have RunContext Client or the function raises "Expected a Script with RunContext set to Client". It then walks the live Luau thread list, follows each thread node's first weak thread reference to LiveLuaRef, skips missing thread data or expired Script stored references, and compares each script reference with the requested script. If it finds a match, it returns that Luau thread as a thread. If no match is found, it returns nil.

Call it with 1 parameter(s): script. The argument table explains which values are required and which ones only refine the behavior.

It returns thread (thread | nil). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Read the Luau thread associated with a supported script Instance when one is available.

local targetScript

for _, descendant in ipairs(game:GetDescendants()) do
    if descendant:IsA("LuaSourceContainer") then
        targetScript = descendant
        break
    end
end

if targetScript then
    local thread = getscriptthread(targetScript)
    print(thread)
end