Lobby Code System

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to make a lobby joining system using randomly generated codes

  2. What is the issue? Include screenshots / videos if possible!
    I am using ReserveServers and correctly getting the access code and inserting the correct access code but I still get a teleport failed error saying “Access code for reserved server is not valid.”

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve searched around every documentation of teleportservice and of discussions and nobody else is having this problem

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I’ve provided the script in question, one thing to note is when the new server is made, when the new player joins a datastore is made using SetAsync with the key being the randomly generated code and the Access Code being the data.

  ReplicatedStorage.Remotes.ChooseServerType.OnServerEvent:Connect(function(player, ServerType, Code)
	if ServerType == "Singleplayer" then
		local ReservedServerId, ReservedServerAccessCode = TeleportService:ReserveServer(game.PlaceId)		
		local Options = Instance.new("TeleportOptions")
		Options.ShouldReserveServer = true
		Options:SetTeleportData({
			["InGame"] = true,
			["Multiplayer"] = false,
			["Code"] = "",
			["AccessCode"] = ReservedServerAccessCode
		})

		TeleportService:TeleportAsync(game.PlaceId, {player}, Options)
	elseif ServerType == "Multiplayer" then
		local NewCode = getRandomString()

		local ReservedServerId, ReservedServerAccessCode = TeleportService:ReserveServer(game.PlaceId)
		print(player.Name .. " has made a new multiplayer server with the access code: " .. ReservedServerAccessCode)
		
		local Options = Instance.new("TeleportOptions")
		Options.ShouldReserveServer = true
		Options:SetTeleportData({
			["InGame"] = true,
			["Multiplayer"] = true,
			["Code"] = NewCode,
			["AccessCode"] = ReservedServerAccessCode
		})

		lobbyCodesStore:SetAsync(NewCode, Options.ReservedServerAccessCode)
		TeleportService:TeleportAsync(game.PlaceId, {player}, Options)
	elseif ServerType == "JoinMultiplayer" then
		if lobbyCodesStore:GetAsync(Code) then
			print("Yeah!")
			
			print("The Access Code is:" .. lobbyCodesStore:GetAsync(Code))
			print("Inserted Code:" .. Code)
			print("Server Code:" .. lobbyCodesStore:GetAsync(Code))
			
			task.wait(2)
			
			TeleportService:TeleportToPrivateServer(game.PlaceId, lobbyCodesStore:GetAsync(Code), {player})
		else
			warn("No lobby found.")
		end
	end
end)

1 Like