Getting the "name" of a value in a table

Hello! So rather than using the traditional leader stats and values stuff like that I am using tables. Each player has their own table within a server data table. Their data is saved and loaded. Within the player’s table I have an inventory table.

Inventory = {
  ex = 0,
  ex2 = 0
}

I would like to get the name of each variable in the table so I can set it to the text of a text label in their inventory (for example I would like to get the “ex” in the above table and set the text label’s text to it) is there a way of doing that?

4 Likes

You could loop through the table and use the index variable (i) to get the key, instead of v, the actual value.

for i,v in pairs(Inventory) do
   local text = i
   local number = v
end
13 Likes

OMG thank you so much. I have been going insane trying to figure this out when all it took was the letter i smh

2 Likes