Scripting Helpers is winding down operations and is now read-only. More info→
Log in to vote

[Solved] Can somebody help me here? [closed]

Asked by 9 years ago

Okay, so here's my script:

playerCount = {}
Players = Game:GetService("Players")

    table.insert(playerCount, Player.Name)
    print(table.concat(playerCount, ", "))

    table.remove(playerCount, Player.Name)
    print (table.concat(playerCount, ", "))

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

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?

1 answer

Log in to vote
Answered by
Unclear 1776 Moderation Voter
9 years ago

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)
Wait, so where in my script would i enter this into? ImmenseKassing 120 — 9y
Wherever you're trying to remove the value from the table. That seems to be the PlayerRemoving event. Unclear 1776 — 9y
Oh, wait nvm ImmenseKassing 120 — 9y