Scripts

getmenv

Returns the environment table associated with a ModuleScript.

Syntax

getmenv(moduleScript: userdata): table

Arguments

NameTypeDescription
moduleScriptuserdataModuleScript object whose environment table should be returned.

Returns

NameTypeDescription
environmenttableEnvironment table associated with the ModuleScript.

Description

Returns the environment table associated with a ModuleScript. The argument must be a ModuleScript object. Invalid objects raise "Invalid Instance", non-ModuleScript objects raise "ModuleScript expected", and ModuleScripts without an available thread raise "invalid thread". The function does not accept Script or LocalScript; use getsenv for those cases.

Call it with 1 parameter(s): moduleScript. 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 ModuleScript.

local moduleScript

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

if moduleScript then
    local environment = getmenv(moduleScript)
    print(environment)
end