Garry's Mod Wiki

debug.setfenv

  table debug.setfenv( any object, table env )

Description

We advise against using this. It may be changed or removed in a future update.

Sets the environment of the passed object.

Unlike setfenv, this also works on any userdata, allowing you to save data stored to it which can be accessed using debug.getfenv.
Userdata seem to intentionally support this & setting/changing it does not affect anything (though unused by gmod / Entities and such don't this)
This can be useful when trying to store data on a IGModAudioChannel, Vector, Angle or any other that doesn't already allow you to store data on it.

Arguments

1 any object
Object to set environment of. Valid types: userdata, thread, function.
2 table env
Environment to set

Returns

1 table
The object

Example

create a new environment and setfenv Display inside it

local newenvironment = {} function newenvironment.log( msg ) print( msg ) end local function Display() log( "yay" ) end debug.setfenv( Display , newenvironment ) Display()
Output:
yay