Table.remove its not working

Hello guys! Today I was helping my friend with a script, its very simple, it creates a table called “playerTable”, and checks every time a player joins, then gets the player character, userid and adds the player to the table, and he needed help when the player left the game, so I did another part that checks every time the player leaves, and removes him from the table using table.remove

but it gave this error while playing:

ServerScriptService.PlotScript:23: invalid argument #2 to ‘remove’ (number expected, got Instance)

If your wondering, heres the script:

local Players = game:GetService("Players")

local playersTable = {}

Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	local userId = player.UserId
	
	table.insert(playersTable, player)
	
	if character then
		local plot = workspace.Plots:FindFirstChild("Plot" .. #playersTable)
		
		plot.Owner.Value = player
		plot.OwnerSign.SurfaceGui.TextLabel.Text = player.Name .. "'s Plot"
		plot.OwnerSign.SurfaceGui.Frame.ImageLabel.Image = Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		
		character:MoveTo(plot.PlotTP.Position)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	table.remove(playersTable, player)
	
	local plots = workspace.Plots
	
	for i, v in plots:GetDescendants() do
		if v:IsA("StringValue") and v.Name == "Owner" then
			if v.Value == player then
				v.Value = nil
				v.Parent.OwnerSign.SurfaceGui.TextLabel.Text = "Empty Plot"
				v.Parent.OwnerSign.SurfaceGui.Frame.ImageLabel.Image = nil
			end
		end
	end
end)

do someone knows why it gives a error? and why it’s meant to be a number if the part table.insert(playersTable, player) can be a instance, value, anything?

It’s because table.remove takes the index of the item to remove, not the item itself.

local i = table.find(playersTable, player)

if (i) then
    table.remove(playersTable, i)
end
3 Likes

Thanks man, I didn’t know about that

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.