getsenv
Gets a script's environment table.
Syntax
getsenv(script: LocalScript | ModuleScript) -> tableParameters
| Parameter | Type | Description |
|---|---|---|
script | LocalScript or ModuleScript | The script |
Returns
| Type | Description |
|---|---|
table | The 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
endAccessing 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!")
endModifying 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