Get List of Variables from Table

Yet another dumb question from me, but I’m in pursuit of knowledge. I’ll get right to the point.

Example:

image

Raw Code
local LuaGlobals = {['assert']=assert,['collectgarbage']=collectgarbage,['error']=error,['getfenv']=getfenv,['getmetatable']=getmetatable,['ipairs']=ipairs,['loadstring']=loadstring,['newproxy']=newproxy,['next']=next,['pairs']=pairs,['pcall']=pcall,['print']=print,['rawequal']=rawequal,['rawget']=rawget,['rawset']=rawset,['select']=select,['setfenv']=setfenv,['setmetatable']=setmetatable,['tonumber']=tonumber,['tostring']=tostring,['type']=type,['unpack']=unpack,['xpcall']=xpcall,['_G']=_G,['_VERSION']=_VERSION}

So let’s say you have a table like that, for example. However, doing #LuaGlobals obviously returns 0. Is there a way that I can get all of the variables from a table?

Thanks

You could use a generic for loop. For every iteration add 1 to a variable.

local n = 0

for name, value in pairs(LuaGlobals) do
    n = n + 1
    print(name)
end

The table is “empty”. If you do print(#LuaGlobals), the result will be 0. So that wouldn’t work. Though you can still reference variables like LuaGlobals._G.

But I’m trying to get a list of every variable “attached” to the table.

Not sure what you are asking. My method gets you the “length” of the table. I am not sure if these is a misunderstanding here.

Are you trying to get the key from the key-value pair? That is what name is.

Take the table I’ve provided and try your script. It won’t work like that.

(Also, how did you color your code here?)

1 Like

you have to do

--- ```lua
--- ```

Just without the dashes

1 Like

Ah ty. Well, I swear I tried this multiple times in the past, but now I guess it works. My bad