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

> 当 Real 可以找到时，返回与 script object 关联的 Lua thread。argument 必须是 object。该 function 会读取该值的 ClassName field，如果该 field 不是 string，会抛出 "not a valid Instance"。接受的 ClassName 值是 Script、LocalScript 和 ModuleScript；任何其他值都会抛出 "Script expected"。它首先通过 script reference 检查 Real 的 ScriptThreadCache。cache hit 时，返回缓存的 Luau thread 作为 thread。cache miss 时，Script 值必须具有 RunContext Client，否则 function 会抛出 "Expected a Script with RunContext set to Client"。然后它遍历 the live Luau thread list，沿着每个 thread node 的 first weak thread reference 到 LiveLuaRef，跳过 missing thread data 或 expired Script stored references，并将每个 script reference 与请求的 script 比较。如果找到 match，则返回该 Luau thread 作为 thread。如果没有找到 match，则返回 nil。

<div className="real-function-page">
  ```lua theme={null}
  getscriptthread(script: userdata): thread | nil
  ```

  ## 参数

  <ParamField path={"script"} type={"userdata"} required>
    要查找的 Script object。接受的 ClassName 值是 Script、LocalScript 和 ModuleScript。
  </ParamField>

  ## 返回值

  <ResponseField name={"thread"} type={"thread | nil"}>
    关联的 Lua thread，或找不到 matching live thread 时为 nil。
  </ResponseField>

  ## 示例

  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
  ```
</div>
