When pre-existing global variables are redefined in Luau by assigning properties to the environment table (getfenv(0)
), optimizations for global variable lookups (i.e. require
) do not get disabled until one of the following expressions are used:
print()
warn()
setfenv(0, getfenv(0)) -- Argument #2 can /likely/ be anything. Haven't verified.
To get a better idea of what I’m talking about, these work:
print("1")
getfenv(0).require = print
require("2")
-- Output:
-- 1
-- 2
getfenv(0).require = print
warn("1")
require("2")
-- Output:
-- 1
-- 2
and this does not:
getfenv(0).require = print
require("this should work")
-- Error:
-- 23:44:41.804 - Attempted to call require with invalid argument(s).
-- 23:44:41.804 - Stack Begin
-- 23:44:41.805 - Script 'ServerScriptService.does not work', Line 2
-- 23:44:41.805 - Stack End
Repro file: repro.rbxl (14.9 KB)