Syntax
local _ENV = { print = print, label = "demo" }Examples
Lua lexical _ENV scopes
Run with standalone Lua 5.4: lua example.lua. These scripts target Lua 5.4 rather than Roblox Luau.
local outerPrint = print
do
local _ENV = {print = outerPrint, label = "isolated"}
print(label)
label = "updated"
print(label)
end
outerPrint("outside")- Output: isolated, updated, outside. Assignment changes the local environment table rather than an outer local.
Best practices
- Capture dependencies before replacing _ENV. Restricting names alone is not a secure sandbox for hostile code.
Runtime & versions
Lua interpreter or a host application. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Lua reference manuals