Unable to cast value to Objects (TeleportService:TeleportAsync)

I want to make a lobby system by myself. I have the problem with TeleportService:TeleportAsync()

Here’s the serverscript inside a part :

-- change settings here

local maxplayers = 2
local maxtimer = 20
local placeID = 17267036049

-- other variables

local list = {}
local playerExistsInLobby = false
local teleportProcessing = false
local timer
local frame = script.Parent.SurfaceGui.Frame
local enterpart = script.Parent.Parent.waitingpoint
local cablecar_basepart = script.Parent.Parent.CableCar.Base

local replicatedstorage = game:GetService("ReplicatedStorage")
local teleportservice = game:GetService("TeleportService")

local server = teleportservice:ReserveServer(placeID)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ShouldReserveServer = true
teleportOptions.ReservedServerAccessCode = server


local function updateplayer ()
	frame.PlayerNum.Text = #list .. "/" .. maxplayers
end

-- start values

frame.PlayerNum.Text = #list .. "/" .. maxplayers

-- running values


replicatedstorage.LeaveGuiEvent.OnServerEvent:Connect(function (player)
	for i = 1, #list do
		if list[i] == player.Character.Name then
			player.Character.Humanoid.Jump = true
			task.wait(.1)
			player.Character.Humanoid.Jump = true
			task.wait(.1)
			player.Character.Humanoid.Jump = true
			player.Character.HumanoidRootPart.CFrame = script.Parent.Parent.ExitLobbyPart.CFrame
			table.remove(list, i)
			if #list < 0 then
				playerExistsInLobby = false
				teleportProcessing = false
			end
			task.wait(.1)
			updateplayer()
		end

	end
end)


-- when touches a wall

script.Parent.Touched:Connect(function (hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if teleportProcessing == false then
			local char = hit.Parent
			local players = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
			local playerenter = false
			-- do things here and check the teleport process or max players

			for i = 1, #list do
				if list[i] == char.Name then
					playerenter = true
				end
			end
			if playerenter == false then
				if #list < maxplayers then
					table.insert(list, char.Name)

					char:FindFirstChild("HumanoidRootPart").CFrame = enterpart.CFrame

					updateplayer()

					print(list)

					replicatedstorage.LeaveGuiEvent:FireClient(players, true)

					for i = 1, #list do
						if #list >= 1 then
							if list[i] == char.Name then
								playerExistsInLobby = true
							end
						else
							playerExistsInLobby = false
						end
					end
				end

				-- check if player left then remove character name from the table
				game:GetService("Players").PlayerRemoving:Connect(function (plrWhoGetsRemovedOrLeft)
					if plrWhoGetsRemovedOrLeft.Name == char.Name then
						for i = 1, #list do
							if list[i] == plrWhoGetsRemovedOrLeft.Name then
								table.remove(list, i)
								updateplayer()

								print(#list , list[1] , list[2])

								if teleportProcessing == true then
									if #list <= 0 then
										teleportProcessing = false
									end
								end
							end
						end
					end
				end)

				-- check if player die or removed then remove character name from the table
				players.CharacterRemoving:Connect(function (charWhoGetsRemovedOrDied)
					if charWhoGetsRemovedOrDied.Name == char.Name then
						for i = 1, #list do
							if list[i] == charWhoGetsRemovedOrDied.Name then
								table.remove(list, i)
								updateplayer()

								print(#list , list[1] , list[2])

								if teleportProcessing == true then
									if #list <= 0 then
										teleportProcessing = false
									end
								end
							end
						end
					end
				end)
			end
		end

	end

end)


while task.wait() do
	for i = 1, maxtimer do
		timer = maxtimer - i
		frame.Timer.Text = timer
		task.wait(1)
	end

	if #list > 0 then
		if playerExistsInLobby == true then
			teleportProcessing = true
			frame.Status.Text = "Teleporting"
			for i = 1, #list do
				if list[i] then
					local player = game:GetService("Players"):FindFirstChild(list[i])

					-- runs when player is exist and being teleported

					replicatedstorage.LeaveGuiEvent:FireClient(player, false)

					task.wait(5)
					
					teleportservice:TeleportAsync(placeID, player, teleportOptions)

					task.wait(5)

					teleportProcessing = false
				end
			end
			repeat task.wait() until not teleportProcessing
			frame.Status.Text = "Ready"

			playerExistsInLobby = false
			for i = 1, #list do
				if list[i] then
					table.remove(list, i)
					updateplayer()
				end
			end

		end
	end
end

Here’s the short line of codes :


local placeID = 17267036049

local teleportservice = game:GetService("TeleportService")

local server = teleportservice:ReserveServer(placeID)
local teleportOptions = Instance.new("TeleportOptions")
teleportOptions.ShouldReserveServer = true
teleportOptions.ReservedServerAccessCode = server


if #list > 0 then
		if playerExistsInLobby == true then
			teleportProcessing = true
			frame.Status.Text = "Teleporting"
			for i = 1, #list do
				if list[i] then
					local player = game:GetService("Players"):FindFirstChild(list[i])

					-- runs when player is exist and being teleported

					replicatedstorage.LeaveGuiEvent:FireClient(player, false)

					task.wait(5)
					
					teleportservice:TeleportAsync(placeID, player, teleportOptions)

					task.wait(5)

					teleportProcessing = false
				end
			end
			repeat task.wait() until not teleportProcessing
			frame.Status.Text = "Ready"

			playerExistsInLobby = false
			for i = 1, #list do
				if list[i] then
					table.remove(list, i)
					updateplayer()
				end
			end

		end
	end

I tried printing the “player” and it gives player name. And tried printing the player classname and it gives “Player” class. I’m stuck with this issue for days. Also the Reserve Server is in teleportOptions. There was an error HTTP 403 Forbidden, but It was an error from roblox studio so that’s not the issue.

may I ask, what you are trying to do exactly? Are you trying to make one of those story game lobbies where you jump into a chair and when the car leaves, any player in a chair gets teleported to the reserved server where the actual game begins?

Yes, but player can sit or stand what they want.

this error occurs when there is an issue with the arguments passed to the TeleportAsync function,in your code you are passing the player variable directly to the TeleportAsync function, but it should be passed as an array of players.

you can modify the code inside the if #list > 0 then block like this:

if playerExistsInLobby == true then
    teleportProcessing = true
    frame.Status.Text = "Teleporting"
    local playersToTeleport = {}
    for i = 1, #list do
        if list[i] then
            local player = game:GetService("Players"):FindFirstChild(list[i])
            table.insert(playersToTeleport, player)
            replicatedstorage.LeaveGuiEvent:FireClient(player, false)
        end
    end

    task.wait(5)
    teleportservice:TeleportAsync(placeID, playersToTeleport, teleportOptions)
    task.wait(5)

    teleportProcessing = false

    repeat task.wait() until not teleportProcessing
    frame.Status.Text = "Ready"

    playerExistsInLobby = false
    for i = 1, #list do
        if list[i] then
            table.remove(list, i)
            updateplayer()
        end
    end
end
1 Like

That actually works. Thank you so much.

1 Like