local V=game:GetService("\084\101\108\101\112\111\114\116\083\101\114\118\105\099\101")local N=5 local q=1 local E=15 local function I(E,I,m)local M=0 local w,a repeat w,a=pcall(function()return V:TeleportAsync(E,I,m)end)M+=1 if not w then task.wait(q)end until w or M==N if not w then warn(a)end return w,a end local function m(V,N,m,M,w)if N==Enum.TeleportResult.Flooded then task.wait(E)elseif N==Enum.TeleportResult.Failure then task.wait(q)end I(M,{V},w)end V.TeleportInitFailed:Connect(m)return I
local TeleportService = game:GetService("TeleportService")

local ATTEMPT_LIMIT = 5
local RETRY_DELAY = 1
local FLOOD_DELAY = 15

local function SafeTeleport(placeId, players, options)
	local attemptIndex = 0
	local success, result -- define pcall results outside of loop so results can be reported later on

	repeat
		success, result = pcall(function()
			return TeleportService:TeleportAsync(placeId, players, options) -- teleport the user in a protected call to prevent erroring
		end)
		attemptIndex += 1
		if not success then
			task.wait(RETRY_DELAY)
		end
	until success or attemptIndex == ATTEMPT_LIMIT -- stop trying to teleport if call was successful, or if retry limit has been reached

	if not success then
		warn(result) -- print the failure reason to output
	end

	return success, result
end

local function handleFailedTeleport(player, teleportResult, errorMessage, targetPlaceId, teleportOptions)
	if teleportResult == Enum.TeleportResult.Flooded then
		task.wait(FLOOD_DELAY)
	elseif teleportResult == Enum.TeleportResult.Failure then
		task.wait(RETRY_DELAY)
	end

	SafeTeleport(targetPlaceId, { player }, teleportOptions)
end

TeleportService.TeleportInitFailed:Connect(handleFailedTeleport)

return SafeTeleport
