Still have questions? Join our Discord server and get real time help.
Log in to vote

How do I get the max index in a table?

Asked by 2 months ago

Hello, I am making a players table so I can easily access players, but here is my problem:

local playersTable = {}

    table.insert(playersTable, table.getn(playersTable) + 1, player)

Once I run this script I get this in the output: table: 0x51474be0bb02eb5c I probably shouldn't use table.getn I'm guessing? If not then what could I use? Sorry if this question is easy, but I look over the developer hub and couldn't find anything

1 answer

Log in to vote
Answered by 2 months ago

You are getting the "strange" output because you are outputting the table itself and not the contents of the table. A simple for loop can fix that:

for i,v in pairs(playersTable) do
    print(v) -- v would be the entry and i is the index (position in the table)

Additionally to get the amount of entries in a table just do #playersTable. In this specific use case you wouldn't need to even specify the index number since new table entries will always default to #table + 1 (which is what you are doing)

Thank you! BRO_ITmeYO 106 — 2mo

Answer this question