Argument 1 missing or nil

Hello developers,
So i encountered this error where it cannot pass through a for i loop. Im not so sure why but it looks like it only lets 1 person through and then breaks with this error message:

Heres the code:

local function teleportPlayers()
	if #list > 0 then
		teleporting = true
		local playersToTeleport = {}
		local teleportTime = 0
		local code = TS:ReserveServer(placeId)
		
		for i=1,#list do	
			if game.Players:findFirstChild(list[i]) then	
				table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
				
				local char = game.Players:findFirstChild(list[i]).Character
				local person = game.Players:findFirstChild(list[i])
				print(playersToTeleport)

				if char then
					char:MoveTo(game.Workspace.tptogameRoomPart.Position)
				end
				
				TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
				
				removeFromList(char)
			else
				table.remove(list,i)	
			end
		end
		game.ReplicatedStorage.Events.TeleportToGame:Fire(playersToTeleport, code, placeId)
		teleporting = false
		list = {}
		if maxplramt.Value > 1 then
			timer = 15
		else
			timer = 10
		end	
	end
end

Line 37 = if game.Players:findFirstChild(list[i]) then

Try this:

local function teleportPlayers()
	if #list <= 0 then
		return
	end
	
	teleporting = true
	
	local playersToTeleport = {}
	local teleportTime = 0
	local code = TS:ReserveServer(placeId)

	for i = 1, #list do
		local playerName = list[i]
		local playerFound = playerName and game.Players:FindFirstChild(playerName)
		
		if playerFound then	
			table.insert(playersToTeleport, playerFound)

			local char = playerFound.Character
			print(playersToTeleport)

			if char then
				char:MoveTo(workspace.tptogameRoomPart.Position)
			end

			TransitionEvent:FireClient(playerFound)

			removeFromList(char)
		else
			table.remove(list, i)	
		end
	end
	
	game.ReplicatedStorage.Events.TeleportToGame:Fire(playersToTeleport, code, placeId)
	
	teleporting = false
	
	list = {}
	
	if maxplramt.Value > 1 then
		timer = 15
	else
		timer = 10
	end	
end

It fixed the argument missing error. But when there are 6/6 players, only a couple players get teleported instead of all of them.

Can you show us when & what you add to the list?

Heres the full script. Everything should be on there

local TS = game:GetService("TeleportService")
local TweenService = game:GetService("TweenService")
local placeId = "11235153047" -- Enter a second Id Place.
local leaveGuiEvent = game.ReplicatedStorage.LeaveGuiEvent
local TransitionEvent = game.ReplicatedStorage.TransitionEvent
local list = {}
local billboard = script.Parent.billboardPart.billboardGui
local timer
local teleporting = false
local spawnTeleport = script.Parent.spawn

local maxplramt = script.Parent:WaitForChild("AmountOfPlayers")

local function updateGui()
	billboard.Frame.players.Text = "Players: " ..#list.. "/"..maxplramt.Value -- Change the number 16 according to the Max Player who want to be teleported.
end

local function removeFromList(character)
	for i=1,#list do
		if list[i] == character.Name then
			table.remove(list,i)
			updateGui()
		end
	end
end

updateGui()

local function teleportPlayers()
	if #list > 0 then
		teleporting = true
		local playersToTeleport = {}
		local teleportTime = 0
		local code = TS:ReserveServer(placeId)
		
		for i=1,#list do	
			if game.Players:findFirstChild(list[i]) then	
				table.insert(playersToTeleport,game.Players:findFirstChild(list[i]))
				
				local char = game.Players:findFirstChild(list[i]).Character
				local person = game.Players:findFirstChild(list[i])
				print(playersToTeleport)

				if char then
					char:MoveTo(game.Workspace.tptogameRoomPart.Position)
				end
				
				TransitionEvent:FireClient(game.Players:findFirstChild(list[i]))
				
				table.remove(list,i)
			else
				table.remove(list,i)	
			end
		end
		game.ReplicatedStorage.Events.TeleportToGame:Fire(playersToTeleport, code, placeId)
		teleporting = false
		updateGui()
		list = {}
		if maxplramt.Value > 1 then
			timer = 15
		else
			timer = 10
		end	
	end
end

script.Parent.Gate.Touched:Connect(function(hit)
	if hit.Parent:findFirstChild("Humanoid") then
		if teleporting == false then
			local char = hit.Parent
			local player = game.Players:FindFirstChild(char.Name)
			local alreadyExists = false
			
			for i=1,#list do
				if list[i] == char.Name then
					alreadyExists = true
				end
			end
			
			if alreadyExists == false then
				if #list < maxplramt.Value then -- Many Players have been teleported.
					table.insert(list,char.Name)
					char.PrimaryPart.CFrame = spawnTeleport.CFrame
					updateGui()
					leaveGuiEvent:FireClient(player)
				end
				
				player.CharacterRemoving:connect(function(character)
					removeFromList(character)
				end)
			end
			
		end
	end
end)

leaveGuiEvent.OnServerEvent:Connect(function(player)
	if player.Character then
		player.Character.HumanoidRootPart.Anchored = false
		wait()
		player.Character.Humanoid.Jump = true
		wait()
		player.Character:MoveTo(game.Workspace.leaveRoomPart.Position)
		removeFromList(player.Character)
	end
end)

while wait() do
	if maxplramt.Value > 1 then
		timer = 15
	else
		timer = 10
	end -- Wait time before teleport change to whawtever you want it to be
	for i=1,timer do
		timer = timer - 1
		billboard.Frame.time.Text = timer
		wait(1)
	end
	teleportPlayers()
end

TL;DR: Remove the line 50 to 53 in the script, so you don’t shrink the list, but if you want to know why, read below but its really long


Before explaining why your code was wrong, I’ll explain how table.remove works, in case you didn’t know:

table.remove(t, index) removes the index and make every index under it go up by 1. So for example:

local Alphabets = {
[1] = "a", 
[2] = "b", 
[3] = "c"}

table.remove(Alphabets, 2)

print(Alphabets)
--[[
{
    [1] = "a",
    [2] = "c" -- Notice how before it was [3] = "c", now it turns into [2] = "c"
}
--]]

Since your script was wrong because of the table.remove line, I’ll give you a second and look at what’s wrong:


Solution:

This is a little bit hard to explain, but we can break your teleportPlayers() function down into 3 sections:

  • Looping through the list
  • Teleport the player
  • Remove the player from the list

Although this looks logically correct, in fact, it is, removing the player from the list shrinks the list length by 1, but your loop is supposed to loop through 1 to 6, assuming there are 6 players. Let’s see a demonstration:


Let’s say you have 6 players, P1, P2, P3… etc

When i = 1, the list length is 6, so the table is currently {P1, P2, P3, P4, P5, P6}, and you teleport P1 and remove him from the list;

When i = 2, the list length is 5, so the table is currently {P2, P3, P4, P5, P6}.
You teleport P3 and remove him from the list, (see why you teleport P3 instead of P2? Because the 2nd value in the current table is P3, not P2) and remove him from the list.

When i = 3, you teleport P5, because the 3rd value in {P2, P4, P5, P6} is P5;
When i = 4, but there are only 3 people in the list, boom errors


Although this looks and sounds really complicated, the solution is just don’t remove him from the list, so the list doesn’t shrink anymore, then you can successfully teleport all the players. Sorry for the long explanation lol.


Solution: Remove line 50 to 53


1 Like

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