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

"table.remove()" is erroring?

Asked by
Discern 1007 Moderation Voter
11 years ago

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)

01playersnotplaying = {}
02 
03for i,v in pairs(game.Players:GetChildren()) do
04    table.insert(playersnotplaying, v.Name)
05end
06 
07randomplayer = playersnotplaying[math.random(1, #playersnotplaying)]
08 
09for i,v in pairs(playersnotplaying) do
10    table.remove(playersnotplaying, randomplayer)
11end

I don't understand why this isn't functioning properly.

1 answer

Log in to vote
3
Answered by 11 years ago

table.remove has two arguments; the table itself, and the numerical index that you want to remove. To fix your code, use the following:

1playersnotplaying = Game.Players:GetPlayers() --This is exactly the same as what you were doing. This is nothing to do with the error, just more efficient.
2randomIndex = math.random(1, #playersnotplaying)
3randomplayer = playersnotplaying[randomIndex]
4table.remove(playersnotplaying, randomIndex) --Remove the index where randomplayer is located.
0
Thanks ,it too made me understand what I was doing wrong, LEARNING IS KEY :P HexC3D 830 — 11y
0
Oh, thanks. I understand it now. :) Discern 1007 — 11y
Ad

Answer this question