Warnings for getfenv/setfenv that their use disables Luau optimisations

Issue Description
Luau optimisations are present in pure environments, however use of getfenv and setfenv mark an environment as impure and disable optimisations. More information can be found via Luau Lang - Performance.

As Roblox continues to develop Luau, it is important that novice developers do not adopt old and bad habits into new work. There are little to no circumstances where using these functions in production is necessary. New developers should be discouraged from using them and so a warning that using these two functions may result in degraded performance seems appropriate.

Issue Area: DevHub Content
Page URL: Lua Globals


Why is getfenv/setfenv disabling performance stuff? There is litterary no replacement and I dont want to redo all my work. there is a debug stuff similar to getfenv however it is buggy and doesnt really do its job

1 Like

This optimization relies on being able to predict the shape of the environment table for a given function; this is possible due to global sandboxing, however this optimization is invalid in some cases:

  • loadstring can load additional code that runs in context of the caller’s environment
  • getfenv / setfenv can directly modify the environment of any function

This is from the article colbert posted. As you can see, in laymans terms, it ‘disables performance stuff’ because some optimizations rely on a predictable environment, of which getfenv and setfenv allow direct modification to.


And then what is a alternative? getfenv is a essential

The only “Real” use case of getfenv is sandboxing loadstring’d code, which generally doesn’t need optimizations anyways. Use the debug library if you need it for debugging purposes.

It’s not like it’s getting removed either. The Roblox engineers know there are people out there that still use getfenv, so they decided to keep it and just remove optimizations for code that uses it.

There is no alternative. Write code that doesn’t rely upon it (what are you doing that requires it?) or accept the performance loss.


Marking this documentation request as solved in favour of the Luau RFC on deprecating fenv (this was brought to my attention via the latest model safety announcement). A deprecation would be a stronger and better indicator that it shouldn’t be pursued in new work: