Error: "invalid argument #1 to 'insert' (table expected, got nil)"

I have an error at line 26 of my code where i am trying to get a table that’s name is the player’s name. The code says “invalid argument #1 to ‘insert’ (table expected, got nil)”

Line 26

table.insert(teleportdata[v.Name], "VColonyValue = " .. v.Character.Colony.Value)

Full code

local teleportservice = game:GetService("TeleportService")
local replicatedstorage = game.ReplicatedStorage
local escapeguione = replicatedstorage:WaitForChild("EscapeGuiOne")

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local humanoid = hit.Parent.Humanoid
		local colony = hit.Parent.Colony
		local arrayofplayers = {
			
		}
		for i, v in pairs(game.Players:GetPlayers()) do
			if v.Character:WaitForChild("Colony").Value == colony.Value and colony.Value ~= "" then
				if game.ReplicatedStorage.ColonyStuff:FindFirstChild(colony.Value).MemberPermsFolder.MembersCanExit.Value == true or game.ReplicatedStorage.ColonyStuff:FindFirstChild(colony.Value).ColonyLeaderValue.Value == humanoid.Parent.Name then
				table.insert(arrayofplayers, v)
				
				teleportdata = {
	             
				}
				
				table.insert(teleportdata,  table.create(1, v.Name))
					for i, v in pairs(teleportdata) do
						print(v)
					end
					
					table.insert(teleportdata[v.Name], "VColonyValue = " .. v.Character.Colony.Value)
					
					for i, v in pairs(teleportdata) do
						print(v)
						for e, t in pairs(teleportdata[v.Name]) do
							print(t)
						end
					end
				
				end
			elseif v.Character.Name == hit.Parent.Name then
				teleportservice:Teleport(98985454249054, game.Players:GetPlayerFromCharacter(hit.Parent))
				print("teleported one player not in a colony")
			end
		end
		
		local teleportoptions = Instance.new("TeleportOptions")
		teleportoptions:SetTeleportData(teleportdata)
		teleportservice:TeleportAsync(98985454249054, arrayofplayers, teleportoptions)
		
	end
end)

You haven’t actually defined teleportdata[v.Name] at all, so it results nil. Looking at your code (line 21), you inserted another table containing the player’s name, but that isn’t a dictionary.
Try replacing line 21 with

teleportdata[v.Name] = {}

and see if it resolves your issue

it just prints the curly brackets instead of the player’s name

The player’s name is the ‘key’ (which is in line with your line 26). In normal arrays, the keys are just numbers (1, 2, 3, …)

If you don’t want to use a dictionary, then undo the change I suggested in the previous post, and replace line 26 with:

for k, t in teleportdata do
	if t[1] == v.Name then
		table.insert(t, "VColonyValue = " .. v.Character.Colony.Value)
	end
end	

I would strongly recommend just using a dictionary, both for the player’s name and the values within the table

@SeargentAUS mentioned it, why not use a dictionary? It doesn’t make much sense to use an array for that kind of stuff and is more confusing than helpful…

Instead of your current script (table.insert(... ... .Value))
I’d recommend writing something like this:

teleportdata[v.Name].VColonyValue = v.Character.Colony.Value

It might also be a good thing to use pcall since you’re checking some data and teleporting players/ adding check to ensure the data actually exists, thus avoiding error.

Please refer to the following article for further information Tables | Documentation - Roblox Creator Hub
Pcalls - When and how to use them

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