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

Why does a TeleportToPrivateServer must be passed an array of players error come up?

Why does this error keep coming up although I'm clearly passing in an array of players already? Basically, I'm trying to create a teleportation system for a game where players can gather in a collider, and once there are enough players, they will be teleported to a private server where they can enjoy the game as a party. The code can be found below.

local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")

local elevators = game.Workspace.Elevators:GetChildren()

local elevatorList = {}


function getListSize(list)
	local elemCount = 0

	for key, value in pairs(list) do
		elemCount += 1
	end

	return elemCount
end


function listContains(list, object)
	for key, value in pairs(list) do
		if value == object then
			return true
		end
	end

	return false
end

function getIndexOfElement(list, object)
	for key, value in pairs(list) do 
		if value == object then
			return key
		end
	end

	return nil
end



function teleportPlayers(listOfPlayers)
	if listOfPlayers ~= nil and getListSize(listOfPlayers) ~= 0 then
		local players = {}
		for key, player in pairs(listOfPlayers) do
			if type(player) == "userdata" then
				players[getListSize(listOfPlayers)] = player
			end
		end
		
		local accessCode = TeleportService:ReserveServer(8402556022)
		TeleportService:TeleportToPrivateServer(8402556022, accessCode, players, nil, getListSize(listOfPlayers))
	end
end

for key, elevator in pairs(elevators) do
	-- Storing a dictionary of each elevator's key info(players inside) in a table array
	elevatorList[elevator] = {{}, false, 10} -- Key: Elevator Object, Value: Array of an array of players in the elevator, and a bool to tell if the elevator is full, and the number of seconds before the elevator teleports


	-- Checking for Collisions with the Entrance

	elevator.Touched:Connect(function (part)
		if part.Parent:FindFirstChildWhichIsA("Humanoid") then
			local playerList = elevatorList[elevator][1]
			local player = Players:GetPlayerFromCharacter(part.Parent)

			if not listContains(playerList, player) then -- If the player isn't already in the array of players		
				elevatorList[elevator][1][getListSize(playerList)] = player
				print("Add player")
			end
		end
	end)


	elevator.TouchEnded:Connect(function (part) -- If an elevator's touch ends
		if part.Parent:FindFirstChildWhichIsA("Humanoid") then 

			-- Remove the player from the array of players in the elevator
			local playerList = elevatorList[elevator][1]
			local player = Players:GetPlayerFromCharacter(part.Parent)

			local indexOfPlayer = getIndexOfElement(playerList, player)

			if listContains(playerList, player) then 
				elevatorList[elevator][1][indexOfPlayer] = nil
				print("Remove player")
			end

		end

	end)


end


while true do
	wait()

	-- Checking if each elevator is full or not 
	for key, elevator in pairs(elevators) do
		local elevatorMaxSize = elevator.Value.Value

		if getListSize(elevatorList[elevator][1]) == elevatorMaxSize and elevatorList[elevator][2] == false then
			elevatorList[elevator][2] = true  -- The elevator is ready to go
			print("Elevator full")
			-- Play the close door animation

		elseif getListSize(elevatorList[elevator][1]) > elevatorMaxSize and elevatorList[elevator][2] == true then
			print("Elevator too full")
			-- if doors are out of place, open the doors and change the ui to too full

			elevatorList[elevator][2] = false  -- The elevator is not ready to go
			
		elseif getListSize(elevatorList[elevator][1]) < elevatorMaxSize and elevatorList[elevator][2] == true  then
			-- If the people in the elevator is less than the max size
			elevatorList[elevator][2] = false
			
		end
		
		
		-- Countdown
		local elevatorFull = elevatorList[elevator][2]

		if elevatorFull == true then
			wait(1)
			elevatorList[elevator][3] -= 1
			print("Counting down ", elevatorList[elevator][1])

		elseif elevatorFull == false then
			elevatorList[elevator][3] = 10
		end

		if elevatorFull == true and elevatorList[elevator][3] <= 0 then
			print("Elevator teleporting... ", elevatorList[elevator][1])
			teleportPlayers(elevatorList[elevator][1])
		end
	end
end

The error occurs at line 52 where I teleport the players. I have no idea why this is not working. Even though the elements of the array clearly contain Player objects, I printed out the type of the object and it says 'userdata'. So I clearly have an array of Player objects, and I am passing it into the function. However, this error still comes up. I have no idea what to do.

2 comments

0

Btw, the way this code works is by checking if the elevator is colliding with any Players, then if they are, add the player to the array of players in the elevator. If the size of the array is equal to the maximum number of players allowd in the elevator, then it will set the boolean, 'elevatorFull' to true, and that will trigger a countdown of 10 seconds before it teleports all the players into a

0

Also, for some reason, the function works when the array only consists of one element, but if there are multiple players, the error pops up.

1 answer

Never mind, it works now. I just changed the players[getListSize(listOfPlayers)] = player to a table.insert function

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