Scripts

getsenv

Returns the environment table associated with a LocalScript, Script, or ModuleScript.

Syntax

getsenv(script: userdata): table

Arguments

NameTypeDescription
scriptuserdataScript object to inspect. Supported ClassName values are LocalScript, Script, and ModuleScript.

Returns

NameTypeDescription
environmenttableEnvironment table associated with the script or module.

Description

Returns the environment table associated with a LocalScript, Script, or ModuleScript. Unsupported script objects or scripts without an available thread raise the same errors as the underlying script lookup.

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

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

Example

Read the environment table associated with a supported script Instance.

local targetScript

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

if targetScript then
    local environment = getsenv(targetScript)
    print(environment)
end