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

playersnotplaying = {}

for i,v in pairs(game.Players:GetChildren()) do
    table.insert(playersnotplaying, v.Name)
end

randomplayer = playersnotplaying[math.random(1, #playersnotplaying)]

for i,v in pairs(playersnotplaying) do
    table.remove(playersnotplaying, randomplayer)
end

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

1 answer

Log in to vote
3
Answered by 10 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:

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.
randomIndex = math.random(1, #playersnotplaying)
randomplayer = playersnotplaying[randomIndex]
table.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 — 10y
0
Oh, thanks. I understand it now. :) Discern 1007 — 10y
Ad

Answer this question