The table returned by getfenv isn't iterable?

While trying to get a list of all the globals in Roblox’s environment, I ran the following code:

for k, v in next, getfenv() do
  print(k, v)
end

This printed: script Script

This notably does not include such globals as string or table, and it doesn’t even include things like game or workspace. What’s weirder is that when I try to directly index getfenv to get a global (such as game or string), it returns it just fine.

Why is this? Does Roblox have a __index set in the metatable for the environment? Is there any way to actually get a list of all globals in Roblox?

The default environment has a metatable that points to objects like string, table, game, workspace, etc.
For security reasons the metatable is locked so you can’t edit it.
You could try setting __index to the default environment.

Yeah it works

local fenv = getfenv()

local meta = {}
meta.__index = fenv

local env = {}
setmetatable(env, meta)

setfenv(1, env)

print(game, workspace, workspace.Baseplate)
1 Like

That’s helpful but it doesn’t really help me in this situation. I was hoping to be able to get every global in the environment without having to copy them manually from the Developer Hub. Oh well.

This would mean breaking out of the sandbox. You would need to copy them from the dev hub.

1 Like

Mind saying why do you need a list of all globals in the first place? Maybe we will find a better way than that. Because indeed, every default env of scripts has an __index set by default, which is a table with all global variables. Since it’s only one table for all scripts, being able to access the __index would give you an ability to spoof functions for other scripts.

As a fun fact I’ll add that there was actually a bug which let you obtain that table due to the way roblox used to handle script errors, but it was patched two years ago. In the script builder community we called it “global override”.

1 Like

I’m making a sandbox and more or less want to be able to white- or blacklist specific globals or group of globals. I’ve gotten around it for the moment by just manually writing out a list of all globals (that are listed) but it was annoying.

You can just make a table with the names of all globals you wish to blacklist. Unless you want to baclkist like 2/3 of them. In that case make a whitelist of names and types, such as, check if it’s an enum or an object such as a Vector3.

This should help Documentation - Roblox Creator Hub

1 Like