Invalid argument #2 to 'remove' (number expected, got Instance)

How do I remove the player from the table when they left, when ever I run the game I keep getting the error.

gameTable.removeImposter = function(removeThisPlayer)
	for i, players in pairs(gameTable.imposters) do
		if players == removeThisPlayer then
			table.remove(gameTable.imposters, i)
		end
	end
end

players.PlayerRemoving:Connect(function(plr)
	if table.find(imposters, plr) then
		removeImposter(plr)
		print(imposters)
	end
end)
1 Like

table.remove requires the index

table.remove(table,table.find(table,value))

4 Likes
players.PlayerRemoving:Connect(function(plr)

for i, v in pairs(simpleTable) do
     if v == plr then
         table.remove(simpleTable, i,v)
     end
end

end

Haven’t tested it out in studio yet so i’m not 100% sure it will work.

2 Likes

What if I used the table.remove() inside the function?

players.PlayerRemoving:Connect(function(plr)
	if table.find(imposters, plr) then
		print(imposters)
		removeImposter(plr)
	end
end)

You can definitely do that. Just make sure the code in the function is working.

The code in the function is working but when I try to remove the player in the table when they left it does not remove them.

It means the code in the function isn’t working then. Also you are printing the table before you are removing. If you want to see the player removed, try printing the table after you have removed the specific player.

I did that so that I would know that they are inside the table, and here is the script for the function:

gameTable.removeImposter = function(removeThisPlayer)
	for i, players in pairs(gameTable.imposters) do
		if players == removeThisPlayer then
			table.remove(gameTable.imposters, i)
		end
	end
end

Please, you NEED to learn how to debug things on your own. This is so simple. Please, read the error carefully, read your code carefully as well.

The issue is that the second argument you passed to table.remove is a value of type Instance - when it should actually be number.

2 Likes

Because you are passing player argument which is passed as nil. Use Player User Id instead.

-- In player added function
table.insert(simpleTable, plr.UserId)

-- In player removed function
table.remove(simpleTable, plr.UserId)
6 Likes

No question is useless, everyone can learn from each other, simplicity is not an excuse.

10 Likes