How to get a Table Key

The title says it all. Is it possible to get the name of a table, or something like that?

Note: before you reply keep these things in mind:

  • I know you can put the table key in the table, however I cba to do that for my game.
  • I am not looking to print out the contents of the table, just get the key of the table.
1 Like

do you mean this?

local t = {
   ['key1'] = {a = b, c = d}
}
2 Likes

What do you mean by ā€œthe name of a tableā€?

1 Like

You canā€™t get a tableā€™s ā€œnameā€ because tables donā€™t have a name to begin with. Variable names does not equal to an objects name.

Only objects in Luau that actually have names are function that are declared with the function functionName()-end syntax where functionName is the name of the function. This name doesnā€™t change even after changing the variable that function is assigned to and setting the old functionName variable to nil.

1 Like

You must make sure the table is a global variable inside your script (i.e donā€™t precede the declaration with ā€˜localā€™). Then, simply use getfenv() and you can find the tableā€™s ā€œnameā€ through there.

exampleTable = {
    randomValue = "this is facts"
}
table.foreach(getfenv(),print) --> exampleTable {}

local tableName = nil
for i,v in pairs(getfenv()) do
    if typeof(v) == "table" then
        print(i) --> exampleTable
        tableName = i
        break
    end
end

However, I advise you not to do this.

not at all, i want the tableā€™s name.

for example:

["hi"] = {}

i want to get the ā€œhiā€.

This is called the key NOT the name. If you want to get The key in a loop, it would look like this:

local tbl = {
    ["Hi"] = "Hello",
    ["Bye"] = "Goodbye",
}

for key, value in pairs(tbl) do
    print(key, "has the value of:", value)
end

Knowing this, if you want to get the key by the value, you can make this function:


local tbl = {
    ["Hi"] = "Hello",
    ["Bye"] = "Goodbye",
}

function GetKeyByValue(Value, Table)
    local Key
    for key, value in pairs(Table) do
        if value == Value then 
            Key = key
            return Key
        end
    end
    return Key
end

local Key = GetKeyByValue("Hello", tbl)
print(Key) -- This should print Hi
4 Likes

lemme explain what im trying to get

["hi"] = {
-- stuff in table
}

i dont want the keys of the stuff in the table, i want the table key

You canā€™t get the table identifier unless you do what @DRCGaming suggested

Ok, I found a way to do what I think you want:

local tbl = {
	["hi"] = {
		["Test1"] = 1,
		["Test2"] = 2
	}
}

function CheckTableEquality(t1,t2)
	for i,v in next, t1 do if t2[i]~=v then return false end end
	for i,v in next, t2 do if t1[i]~=v then return false end end
	return true
end

function GetKeyByValue(Value, Table)
	local Key
	for key, value in pairs(Table) do
		if CheckTableEquality(value, Value) then 
			Key = key
			return Key
		end
	end
	return Key
end

local Key = GetKeyByValue({["Test1"] = 1, ["Test2"] = 2}, tbl)
print(Key) -- This should print hi

If you supply the value you are looking for as the first parameter and the table you are trying to find it in, it should return the key that stores that first value!

Credit to: @Aldanium for the CheckTableEquality function.

1 Like

ty, im requesting to lock this post now

1 Like