First you need is sort your table:
local myTable = {
{id = 1, points = 100},
{id = 3, points = 200},
{id = 4, points = 150},
{id = 7, points = 150}
}
You don't need to index those values as 'player1', 'player2', etc. So if you want the index(es) to appear as player1, etc. return the index and concatenated as player, like:
for key, value in pairs(myTable) do
print('player' .. key)
end
The past code will print all table indexes with prefix 'player'.
But to get only the first two values; what we can do? Simple! Make a function:
local myTable = {
{id = 1, points = 100},
{id = 3, points = 200},
{id = 4, points = 150},
{id = 7, points = 150}
}
local function collect(table, howMuch)
local response, index = {}, 1
for key, value in pairs(table) do
response[key] = howMuch and index <= howMuch and value or nil
index = index + 1
end
return response
end
local response = collect(myTable, 2)
print(response[1].id, response[2].id, response[3]) -- 1, 3, nil
What this function do?
Collect values from the first argument named table, and in base the second argument named howMuch (as a max of the response) return those values.
Or for friends: iters table myTable and only returning a table containing 2 fields.