Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

"table.remove()" is erroring?

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

✓ Accepted answer

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.

2 comments

0

Thanks ,it too made me understand what I was doing wrong, LEARNING IS KEY :P

0

Oh, thanks. I understand it now. :)

This discussion is preserved as it was. New answers, comments, and votes are closed.