setfenv
Example
Use setfenv to create a sandbox
local sandbox = {}
-- Define a print function in the sandbox
sandbox.print = function(s)
return print("[Sandbox] " .. s)
end
local sandboxedCode = function()
print("Hello")
string.format("%s", "error please")
end
-- change environment for sandboxedCode to the sandbox table
setfenv(sandboxedCode, sandbox)
sandboxedCode()
Output:
[Sandbox] Hello
Script:11: attempt to index global 'string' (a nil value)