How to get the absolute default function environment for a roblox script?

I’m making custom commands, and for the most part it works. I use setfenv to parse arguments through loadstring, and by using setfenv I lose the ability to use many common things, such as Color3, print, and many other extremely basic things.
Is there a way for me to get the absolute default environment for roblox lua? Or am I going to have to type them all in manually?
(getfenv(0) only returns the scope of the script itself, not the built-in methods and classes)

:thinking:

You might want to create a copy of the environment at the very start, since the environment is a table, and tables are passed by reference, any new globals would be added to the table.

Yes but getfenv(0) only returns the top-most scope of the script itself (which only includes .script as a reference to the script instance), the basic methods seem to exist outside that, kind of like a metatable __index
Yeah hold on I think it’s literally a metatable lemme test something

It is a metatable, like you said the only “real” global in the environment is script.

print(getfenv().game) -- game
print(rawget(getfenv(), "game")) -- nil

Welp, it is, but it’s also locked so I don’t believe I can even read it. Darn

1 Like

Yeah. I can’t get a table of just all the default methods as far as I know because it’s locked. Guess I’ll just have to write them all in manually… that’s frustrating

1 Like

Alright I got it

setfenv(func, setmetatable({target = target}, {__index = getfenv(0)}))()

Essentially fighting fire with fire.

2 Likes