Volt

getsenv

Gets a script's environment table.

Syntax

getsenv(script: LocalScript | ModuleScript) -> table

Parameters

ParameterTypeDescription
scriptLocalScript or ModuleScriptThe script

Returns

TypeDescription
tableThe script's environment

Description

getsenv returns the environment table (_ENV) of a script, giving access to all its local and global variables.

Example

local scripts = getrunningscripts()
if #scripts > 0 then
    local env = getsenv(scripts[1])

    print("Variables in script:")
    for key, value in pairs(env) do
        print("-", key, "=", type(value))
    end
end

Accessing Script Variables

local function getScriptVariable(scriptName, varName)
    for _, script in ipairs(getrunningscripts()) do
        if script.Name == scriptName then
            local env = getsenv(script)
            return env[varName]
        end
    end
    return nil
end

local playerData = getScriptVariable("MainScript", "PlayerData")
if playerData then
    print("Found player data!")
end

Modifying Script Variables

local env = getsenv(targetScript)
if env then
    -- Modify a variable
    env.SomeFlag = true

    -- Call a function from the script
    if env.SomeFunction then
        env.SomeFunction()
    end
end

On this page