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

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

I'm trying to make a teleporter that teleports the player to a different place when they're in the teleporter. But if the player leaves the teleporter and the time runs out, I get this error when trying to remove the player from the table "PlayersToTP" bad argument #2 (number expected, got Object) on line 47. This is my code that I have:

local PlayerAmount = 10 -- How many players have to be in a teleporter for it to teleport
local TeleportService = game:GetService("TeleportService")
local reserveServerCode = TeleportService:ReserveServer(4820164617)
local PlayerNum = script.Parent.PlayersInTeleporter -- How many players are in the teleporter
local GuiVisible = game.ReplicatedStorage:WaitForChild("GuiVisible")
local Button = game.StarterGui.ScreenGui.TextButton
local debounce = false
script.Parent.Table.Value = false

PlayersToTP = {}


script.Parent.SurfaceGui.TextLabel.Text = "Players: 0/10"

PlayerNum.Changed:Connect(function()
	script.Parent.SurfaceGui.TextLabel.Text = "Players: "..PlayerNum.Value.."/10"
end)


script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
		debounce = true
		local Humanoid = hit.Parent.HumanoidRootPart
		local char = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(char)
		GuiVisible:FireClient(player)
		Humanoid.CFrame = CFrame.new(game.Workspace.TeleportPart.Position + Vector3.new(0,3,0))
		PlayerNum.Value = PlayerNum.Value +1
		table.insert(PlayersToTP,player)
		
		for i = 30,0,-1 do
			wait(1)
			script.Parent.SurfaceGui.Timer.Text = "Time Left: "..i
			if i == 0 then
				for _, v in pairs(PlayersToTP) do
					if PlayersToTP ~= nil then
						TeleportService:TeleportToPrivateServer(4820164617, reserveServerCode, PlayersToTP)
					elseif PlayersToTP == nil then
							print("No one is in the teleporter")
							script.Parent.SurfaceGui.Timer.Text = "Time Left: 30"
					end
				end
			end
		end
		
		if script.Parent.Table.Value == true then
			table.remove(PlayersToTP, player)
		end

If somebody could help me that would be great! Thanks!

1 answer

✓ Accepted answer

That is because table.remove() takes the index as the second argument to remove the object, not the object to remove

So you could simply do something like this:

(Starting on line 46)

if script.Parent.Table.Value == true then
	for i, v in pairs(PlayersToTP) do
		if v == player then
			table.remove(PlayersToTP, i)
		end
	end
end

That's it, please let me know if you have any questions

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