So I have this fragment of a script here. For some reason, table.remove isn't functioning.
The output says: "bad argument #2 to 'remove' (number expected, got string)
01 | playersnotplaying = { } |
02 |
03 | for i,v in pairs (game.Players:GetChildren()) do |
04 | table.insert(playersnotplaying, v.Name) |
05 | end |
06 |
07 | randomplayer = playersnotplaying [ math.random( 1 , #playersnotplaying) ] |
08 |
09 | for i,v in pairs (playersnotplaying) do |
10 | table.remove(playersnotplaying, randomplayer) |
11 | end |
I don't understand why this isn't functioning properly.
table.remove has two arguments; the table itself, and the numerical index that you want to remove. To fix your code, use the following:
1 | playersnotplaying = Game.Players:GetPlayers() --This is exactly the same as what you were doing. This is nothing to do with the error, just more efficient. |
2 | randomIndex = math.random( 1 , #playersnotplaying) |
3 | randomplayer = playersnotplaying [ randomIndex ] |
4 | table.remove(playersnotplaying, randomIndex) --Remove the index where randomplayer is located. |