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

bad argument #2 (number expected, got Object) error when trying to remove a player from a table?

Hi, I'm trying to make a block of code where when the player touches a brick it removes them from the "PlayersToKill" table. But when I run the code I get this error. bad argument #2 (number expected, got Object) on line 21. I'm not sure what this means or how to fix it but if somebody could help me that would be great!

game.ReplicatedStorage.Events.TeleportToCafe.OnServerEvent:Connect(function()
	PlayersToKill = {}
	for i,v in pairs(game.Players:GetPlayers()) do
		Player = v
		table.insert(PlayersToKill,Player)
	end
	
	local debounce = false
	
	script.Parent.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if debounce == false then
				if game.ReplicatedStorage.CanTeleportToCafe.Value == true then
					debounce = true
					game.ReplicatedStorage.Events.TeleportScreen:FireClient(Player)
					wait(0.7)
					local HumanRootPart = hit.Parent:FindFirstChild("HumanoidRootPart")
					HumanRootPart.CFrame = CFrame.new(game.Workspace.Cafe.Position + Vector3.new(0,3,0))
					print("Teleported")
					
					table.remove(PlayersToKill,Player)
					
					wait(1)
					debounce = false
					
					
				end
			end
		end
		
	end)
		
	wait(7)
for i,v in pairs(PlayersToKill) do
		local Player = v
		for i,v in pairs(game.Workspace:GetChildren()) do
			if v:IsA("Model") and v.Name == Player.Name then
				local SecondPlayer = v
				SecondPlayer.Humanoid.Health = 0
			end
		end
	end
end)

2 answers

table.remove takes in 2 arguments, the first is the table, and the second being the position of the item, also known as the index of the item, you want to remove.

You get the error because your second argument is a Player object, instead of a number.

table.remove(PlayersToKill,Player)

To get the index of the item you want to remove, you can use table.find(tab, item), where tab is the table you want to search and item is the item you want to look for. table.find will return the index of item if item is present inside tab. You can use this to remove Player from PlayersToKill like so:

local index = table.find(PlayersToKill, Player)
table.remove(PlayersToKill, index)

However you have to also take into account the situation where Player is already removed from PlayersToKill. In that case, table.find will return nil because Player no longer exists in PlayersToKill index. So you just use an if statement to check if index is nil to fix this:

local index = table.find(PlayersToKill, Player)
if index then
	table.remove(PlayersToKill, index)
end

You can find the functionality for table functions such as table.remove and table.find here: https://developer.roblox.com/en-us/api-reference/lua-docs/table

When removing an object from a table, you have to tell the script where the object is placed inside the table. I suggest doing this to find it:

for i,v in pairs(PlayersToKill) do
	if v == Player then
		table.remove(PlayersToKill,i)
		break
	end
end

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