Teleport does not work

Hi, I started studying TeleportService today some hours ago, and I was reading this article: Teleporting Between Places,
I went to test the codes. I put the TeleportModule in ReplicatedStorage with this code:

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

local TeleportModule = {}

local RETRY_DELAY = 2
local MAX_WAIT = 10

-- Create remote event instance
local teleportEvent = Instance.new("RemoteEvent")
teleportEvent.Name = "TeleportEvent"
teleportEvent.Parent = ReplicatedStorage

function TeleportModule.teleportWithRetry(targetPlaceID, playersTable, teleportOptions)
	local currentWait = 0

	-- Show custom teleport screen to valid players if client event is connected
	teleportEvent:FireAllClients(playersTable, true)

	local function doTeleport(players, options)
		if currentWait < MAX_WAIT then
			local success, errorMessage = pcall(function()
				return TeleportService:TeleportAsync(targetPlaceID, players, options)
			end)
			if not success then
				warn(errorMessage)
				-- Retry teleport after defined delay
				wait(RETRY_DELAY)
				currentWait = currentWait + RETRY_DELAY
				doTeleport(players, teleportOptions)
			end
		else
			-- On failure, hide custom teleport screen for remaining valid players
			teleportEvent:FireAllClients(players, false)
			return true
		end
	end

	TeleportService.TeleportInitFailed:Connect(function(player, teleportResult, errorMessage)
		if teleportResult ~= Enum.TeleportResult.Success then
			warn(errorMessage)
			-- Retry teleport after defined delay
			wait(RETRY_DELAY)
			currentWait = currentWait + RETRY_DELAY
			doTeleport({player}, teleportOptions)
		end
	end)

	-- Fire initial teleport
	doTeleport(playersTable, teleportOptions)
end

return TeleportModule

And then I created a tunnel:
image

And I put this code in the script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
 
local teleportPart = script.Parent
local targetPlaceID = 6074153281  -- Change this to your place ID
 
-- Require teleport module
local TeleportModule = require(ReplicatedStorage:WaitForChild("TeleportModule"))
 
local function onPartTouch(otherPart)
	-- Get player from character
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)
 
	if player and not player:GetAttribute("Teleporting") then
		player:SetAttribute("Teleporting", true)
 
		-- Teleport the player
		local teleportResult = TeleportModule.teleportWithRetry(targetPlaceID, {player})
 
		player:SetAttribute("Teleporting", nil)
	end
end
 
teleportPart.Touched:Connect(onPartTouch)

And when I went to test it happened:


And I tried to fix the code by putting a debounce:

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

local teleportPart = script.Parent
local targetPlaceID = 6706115859  -- Change this to your place ID

-- Require teleport module
local TeleportModule = require(ReplicatedStorage:WaitForChild("TeleportModule"))

local playersDebounce = {}
local function OnPartTouch(otherPart)
	-- Get player from character
	local player = Players:GetPlayerFromCharacter(otherPart.Parent)

	if player then
		if not playersDebounce[tostring(player)] then
			playersDebounce[tostring(player)] = {debounce = false}
		end
		if not playersDebounce[tostring(player)].debounce then
			playersDebounce[tostring(player)].debounce = true
			print('Teleporting Player...')
			TeleportModule.teleportWithRetry(targetPlaceID, {player})
		end
	end
end

local function OnPlayerRemoving(player)
	if playersDebounce[tostring(player)] then
		playersDebounce[tostring(player)] = nil
	end
end

Players.PlayerRemoving:Connect(OnPlayerRemoving)
teleportPart.Touched:Connect(OnPartTouch)

And it worked, teleported the player, but gave this error:


This error is happening all the time and I can’t solve it (and it’s not my internet)

It teleported the player, it must be your connection not a Roblox error.

I will ask for help to a friend

I think I discovered the problem, when I put the player to teleport to a random public server, the error is because there is no server when it leaves, so you need to have more than one person in the game, correct?

ID 17 is a literal connection error. Something on ROBLOX’s part or your part going wrong.

I’d try testing in a server with more than one person.

I’ve used similar methods to achieve the same end goal and it has worked fine.

1 Like

You can’t use tostring on an instance. (That’s not the problem though.)