> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# getscriptthread

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

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.

## Syntax

```lua theme={null}
getscriptthread(script: userdata): thread | nil
```

## Arguments

| Name     | Type       | Required | Description                                                                                    |
| -------- | ---------- | -------- | ---------------------------------------------------------------------------------------------- |
| `script` | `userdata` | Yes      | Script object to look up. Accepted ClassName values are Script, LocalScript, and ModuleScript. |

## Returns

| Name     | Type            | Description                                                          |
| -------- | --------------- | -------------------------------------------------------------------- |
| `thread` | `thread \| nil` | Associated Lua thread, or nil when no matching live thread is found. |

## Example

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

```lua theme={null}
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
```
