Search code examples
lualua-table

LUA - Getting values from nested table


I have a table which will be used to store each players name, id and another value

{
                    {
                    rpname  =       "name",
                    SteamID =       "STEAM_0:0:",
                    giftsFound      =       "1",
                            },

The table is being sent from server to client via net.ReadTable()

I want to be able to choose each value seperatley but when I have tried the following below it only returns the first letter of every value instead of the first value

for k, v in pairs(tableData) do
        for k, v in pairs(v) do
             print(v[1]
        end
end

Please could somebody help me out?


Solution

  • If I understood correctly, the sample table you wrote in the first block of code would be what you called tableData in the second, right? So, what you want to have is:

    1. An array of players
    2. Each entry in this array is a table
    3. A way of getting each field of each player

    With a few tweaks we can make your code more readable and, from that, correct it. Firsly, I would rename some things:

    • Rename your table players, for it is an array of players
    local players = {
       {
          rpname = "john",
          SteamID = "STEAM_0:0:1",
          giftsFound = "4",
       },
       -- [...]
    }
    
    • Rename your variables in the for-loop
    • In Lua it is common pratice to use _ to name variable we are not going to use. In this case, the key (originally named k) is not something we will use.
    • Since it is a list of players, each entry is a player, so it is logical to rename the variable v to player.
    • Also, I changed pairs() to ipairs() and there's a good reason for that. I won't cover it here, but here it is explained as best as I could. Rule of thumb: if your table is array-like, use ipairs(); else, use pairs().
    for _, player in ipairs(players) do
       -- [...]
    end
    
    • For the nested for-loop, it does make sense using k, v and pairs, so it would be something like this:
    for k, v in pairs(player) do
       print(k,v)
    end
    

    Running the full piece would produce this:

    rpname  john
    giftsFound      4
    SteamID STEAM_0:0:1
    

    I suppose it solves your problem. The real errors in your code were the way you tried to access the nested table field and, arguably, naming variables with names you have already used (k and v) in the same scope, which is, in the best case, misleading.

    If you want to access a specific field in the table, instead of going through the whole thing, you can do:

    -- To print every player's name
    for _, player in ipairs(players) do
       local name = player.rpname
       print(name)
    end
    

    Or even:

    -- To get the first player's (in the array) name
    local name = players[1].rpname
    

    One last thing: "Lua" is not an acronym, you don't need to use all capital letters. Lua was created in Brazil and here we speak portuguese. Lua means Moon in portuguese.