Garry's Mod Wiki

Garry's Mod Wiki

  function setfenv( function location, table environment )

Description

Sets the environment for a function or a stack level. Can be used to sandbox code.

Arguments

1 function location
The function to set the environment for, or a number representing stack level.
2 table environment
Table to be used as the the environment.

Returns

1 function
The function passed, otherwise nil.

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)

This site is a community mirror of the official Garry's Mod wiki.. This site is not maintained by Facepunch Studios.

Page content is automatically updated twice a day. Edits and history are not available.

Last Parsed: Loading...