Teleport failed because Access code for reserved server is not available

I have written this script which teleports the player to a reserved server depending on whether they make their server public or private.

If you want the full code, let me know but I believe this is the code that has been causing the error:

TextBoxCode.OnServerEvent:Connect(function(player, code)
	local plr = Players:GetPlayerByUserId(player.UserId)

	if plr then
		if game.PrivateServerId == code then
			return
		end

		local HasCode = false
		local Index

		for i, v in ipairs(Servers) do
			if v.plr == plr.UserId and v.id == code then
				HasCode = true
				Index = i
				break
			end
		end

		if HasCode then
			local success, errorMsg = pcall(function()
				local data = serverData:GetAsync(ServerID)

				if data then
					CurrentServerNumber = data

					if CurrentServerNumber > 0 then
						ServerUpdate(code, plr.UserId, CurrentServerNumber)
						local maximumServers = 50
						for i = 1, maximumServers do
							if not table.find(Servers, i) then
								CurrentServerNumber = i
								ServerUpdate(code, plr.UserId, i)
								TeleportService:TeleportToPrivateServer(ServerID, i, {plr})
								break
							end
						end
					else
						local maximumServers = 50

						for i = 1, maximumServers do
							if not table.find(Servers, i) then
								CurrentServerNumber = i
								ServerUpdate(code, plr.UserId, i)
								TeleportService:TeleportToPrivateServer(ServerID, i, {plr})
								break
							end
						end
					end
				else
					local maximumServers = 50

					for i = 1, maximumServers do
						if not table.find(Servers, i) then
							CurrentServerNumber = i
							ServerUpdate(code, plr.UserId, i)
							TeleportService:TeleportToPrivateServer(ServerID, i, {plr})
							break
						end
					end
				end
			end)

			if not success then
				warn('Could not save data, with error ' .. errorMsg)
			end
		end
	end
end)

I have tried to fix the error but it has stayed the same, help would be appriciated!

Are you passing through the id for the reserved server? I don’t see the ServerID variable getting created anywhere.

1 Like

I second this but, have you (@TheSpecialNone) even tried the ':ReserveServer?" it returns the AccessCode itself.

What you are doing is trying to get the number of the servers for the second argument of ''TeleportToPrivateServer; of which, will never work. The second argument MUST be the AccessCode for that specific server.

To what snowing is referencing is how you actually get that code. Of which, most use TeleportData or the MessagingService (SubscribeAsync/PublishAsync).

1 Like

I have managed to fix this in my newer help post in #help-and-feedback I will mark this reply as the solution so people don’t reply with possible answers

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