Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I get the max index in a table?

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

local playersTable = {}

game.Players.PlayerAdded:Connect(function(player)
	table.insert(playersTable, table.getn(playersTable) + 1, player)
end)
print(playersTable)

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

✓ Accepted answer

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)
end

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)

1 comment

0

Thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.