Okay, so here's my script:
playerCount = {} Players = Game:GetService("Players") Players.PlayerAdded:connect(function(Player) table.insert(playerCount, Player.Name) print(table.concat(playerCount, ", ")) end) Players.PlayerRemoving:connect(function(Player) table.remove(playerCount, Player.Name) print (table.concat(playerCount, ", ")) end)
Now, as you can see, if I have two players enter, and then have one leave, the table.remove event cause an error, because I have to have a number instead of Player.Name. If you can see what i was trying to do, I was trying to remove the player's name out of the table playerCount. But of course, you can't do it like that. You would have the number of where the player's name inside the table would be and replace Player.Name with that number.
So, I was wondering, how would you find the number of where the player's name is, so I could remove it whenever they left?
(I am sorry if this isn't that descriptive or anything. I'm just having a hard time explaining this, and hope you can understand well.)
You're using an array-based table. This is a table with only numeric keys.
table.insert
and table.remove
are functions that affect arrays, so naturally they would require numerical keys as inputs.
table.insert(tableReference [, targetIndex], value) table.remove(tableReference, targetIndex)
All values are stored in a table with an appropriate index or key. You can only get or edit the value if you know the key. You can't use table.remove
if you only know the value of what you're trying to remove.
So what I would do is to start a for loop and go through each element of the table, checking if it had the target value.
for index = 1, #playerCount do if playerCount[index] == Player.Name then -- Now we know the correct index attached to the value so we can use table.remove table.remove(playerCount, index) break end end
Locked by TheMyrco
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?