For example, instead of looping through every single player in a table like so
local players = {"player1", "player2"}
game.Players.PlayerAdded:Connect(function(plr)
table.insert(players, #players + 1, plr.Name) --Getting number of indexed values, and adding 1 to get the next position
local found = false
for k, v in pairs(players) do
if v == plr.Name then
found = true
break
end
end
end)
I could just jump straight to the index to which it is located, through the userid
local players = {}
game.Players.PlayerAdded:Connect(function(plr)
table.insert(playertable, plr.UserId, plr.Name) --jumping straight to a free position, as userid's are unique
local found = false
if players[plr.UserId] == plr.Name then
found = true
end
end)
In other words, what is the highest a value can be indexed at in a table? Studio crashes if the number is too big

1 comment
Also, if a player leaves the game, you should find their position in the table and remove their UserId. By doing this, you can save a bit on memory. (If you set that key to nil, the garbage collector will eventually pick it up and reallocate the memory used). Bit of a micro-optimization, but might actually be of use in a large game with lots of players. (Especially in saving time for the loops)