What does mytable[plr] mean

mytable = {}
game.Players.PlayerAdded:Connect(function(plr)
mytable[plr] ---whats this mean?
end

Adding square brackets with a key inside of it is a way of getting (or setting) that particular index within the table.

local myTable={["this is a key"]=1234
print(myTable["this is a key"]) --prints 1234
2 Likes

That’s not actually a valid expression. You’ll have to finish it by either reading from the table or writing to it, or doing something with the value that mytable[plr] refers to (such as calling it, if it’s a function).

Here are examples of those things, as well as some situations where it might be useful:

Storing player join times

playersJoinTimes = {}
game.Players.PlayerAdded:Connect(function(player)
  playersJoinTimes[player] = os.time() --store the time in seconds that the player joined at
end

Storing player data

playersDataTables = {} --notice the plural 's'
game.Players.PlayerRemoving:Connect(function(player)
  local playerDataTable = playersDataTables[player] --singular, no plural 's'
end

Referencing objects associated with player

local player = --some reference to a player
playersTycoons = {} --each player owns a tycoon, referenced by the player object
player.CharacterAdded:Connect(function(character)
  local ownedTycoon = playersTycoons[player]
  ownedTycoon.OwnerSpawned:Fire(character) --ownedTycoon.OwnerSpawned would be a BindableEvent
end)

In all of these examples, the table is actually a dictionary, where the keys are Player objects and the values are some kind of data associated with the relevant player. You can use any kind of value (except nil) as the key to a dictionary, including objects like Players or Parts, or even other tables.

It’s not a valid expression when written in code but I assume that OP was primarily asking what doing [plr] on a table means, because that assignment can be any valid datatype. A good catch for the sake of information but I don’t think this is relevant to what OP was thinking about.

2 Likes

Well, that wouldn’t work, or well like, you should use, table.insert(mytable, plr)