2

I'm trying to get table key name from a value. tostring only returns table: XXXXXXXXX

I tried some functions but nothing work.

config = {
    opt1 = "etc..."
}
players = {}

function openMenu(playerName, configTable)
    players[playerName] = Something to get Table Key...

    -- read the table and create a gui not yet made
end

And next, if I do this :

print(players[playerName])

I want to get this output :

"config"
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
NathaanTFM
  • 83
  • 2
  • 8

2 Answers2

4

You will need to iterate over all pairs of the table and return the key if the value is equal. Note that this will only return one binding, even if multiple keys can lead to the same value:

function find(tbl, val)
    for k, v in pairs(tbl) do
        if v == val then return k end
    end
    return nil
end
llogiq
  • 13,815
  • 8
  • 40
  • 72
-1
table.find(t, value [,start_index]) -> [key or nil]
Fabrizio Stellato
  • 1,727
  • 21
  • 52