0

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?

Sleeps
  • 3
  • 1
  • Can you show which part of the table you posted is assigned to the value `tableData`? If the table you posted is called `tableData`, you can get SteamID of the first entry using this: `tableData[1].SteamID`, as for the for loop, I can't debug it without seeing the output and the actual table. Are you using a custom API or library? My first thought was that the reason you were getting the first letters of each value was that you were passing a string to pairs, but from my tests I've determined it's not possible to pass strings to pairs (meaning the problem would be something else). – Allister Jan 19 '20 at 01:54

1 Answers1

0

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.

Pedro Alves
  • 185
  • 1
  • 10