Creating Reserved Server and entering 5 digit code to enter

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 want to make a system where you enter a 5 digit code, you can join the Reserved Server.

  2. What is the issue? Include screenshots / videos if possible!
    I can’t figure out how to make this system, with the 5 digit code and everything.

  3. What solutions have you tried so far? Did you look for solutions on the developer forum?
    Yes I have looked for solutions, but they haven’t helped unfortunately.

Anything helps, if you find anything please comment it. Thank you.

1 Like

Not much to go on, but based on what you are looking for, here’s what you need to have.

  • You need a lobby system on an open server. This is so players can enter the code.
  • You need to generate the code. This can be done with the Random class.
  • You need a way for players to get the code. You can communicate this directly or you can hide the code in some random location in the lobby. Shrek in the Backrooms posts the code on a piece of paper that tacked to a wall in the level 1 maze. You need that code to open the door to get to level 2.
  • Then you have to use the teleport service to get the players to the reserved server.

I’ll leave the implementation details as an exercise for you to do.

yeah i had to make 2 data stores one for the Servers, so the private server could access its own join code. In the lobby there would be a join gui if they entered a code it would check another datastore “CodesExist” to see if the server exists and teleports them there

edit: sorry if this is hard to understand

  1. I had to make a datastore called “PrivateServerInfo” that would store teleport info and stuff and save it to the privateserverid key that is returned from Tpservice:ReserveServer(). (When they make the server)
  2. Problem was is that i could not access that datastore in the lobby if someone entered a code because the key is the privateserverid, which i do not know how to retrieve. Which is why I made a second datastore called “CodesExist”. (When they use the join code, cant access the datastore because i made its key the privateserverid)

I just dont want 2 datastores, but thats the only way its working

They make the server, it uses a datastore “PrivateServerInfo” to store its own join code. “CodesExists” saves that code, so when someone enters it, it checks “CodesExists” datastore to see if the code exists and teleports them. (CodeExists:GetAsync(code_inputted_by_player), then see if it exists then tp)

On the second server (reservedserver of course), the privateserver grabs its own privateserverid uses it on the PrivateServerInfo datastore and gets its own joincode.
PrivateServerInfo:GetAsync(privateserverid)

You could use the join code as the key to a datastore that hold the data of the server instance, the server ID, and the access code. This code must be ran on the server your lobby is on, but only when creating new servers. I do not know how long Roblox keeps reserved server Id’s and access codes around. But the following code should get you started.

-- ******** Requirements

-- Required game facilities and services
local datastoreService = game:GetService("DataStoreService")
local teleportService = game:GetService("TeleportService")

-- Datastore Access
local datastoreCode = datastoreService:GetDataStore("ServerCodes")


-- ******** Local Data
local rand = Random.new(tick())



-- ******** Functions/Methods

-- Reads data from the data store and returns it.  Returns false
-- if there was an error and prints a warning message to the
-- server log.
local function readDatastore(code)
	local success, result = pcall(function()
		local rData = datastoreCode:GetAsync(code)
		return rData
	end)
	if success then
		return result
	else
		warn("readDatastore:", result)
		return false
end

-- Writes data to the datastore.  Returns true if it was successful
-- or false if there was an error.  Also writes a warning message
-- to the server log upon error.
local function writeDatastore(code, serverId, accessCode)
	data = {
		server = serverId;
		access = accessCode;
	}
	local success, message = pcall(function()
		datastoreCode:SetAsync(code, data)
	end)
	if success then
		return true
	else
		warn("writeDatastore:", message)
		return false
	end
end

-- Generates a random 5-digit code and returns it as
-- a string with leading zeros if necessary to make it
-- 5 digits.
local function generateCode()
	local codeNumber = rand:NextInteger(0, 99999)
	local code = string.format("%05d", codeNumber)
end

-- This is the main code for the script; encapsulated in
-- a function.
local function codeRunner()
	local code = generateCode()
	local dsData = readDatastore(code)
	while dsData ~= nil do
		task.wait(2.0)
		code = generateCode()
		dsData = readDatastore(code)
	end

	local accessCode, serverId = teleportService:ReserveServer()
	print("Server ID:", serverId)
	print("Access Code:", accessCode)
	writeDatastore(code, serverId, accessCode)
end

-- Run the script.
codeRunner()

You run this code for how many access codes and reserved servers that you want to create.
However, to my knowledge, the ONLY way to get a reserved server access code is upon creation of a reserved server via TeleportService:ReserveServer() which returns both the server Id and the access code. You can have multiple servers with the same ID and access code, each one with a different game.JobId. You could teleport to a specific job ID, but the issue is that once that particular server instance is spun down, the job ID is no longer valid, and will never be valid again. Now within the private server, you can get the private server ID.

You only need 1 datastore to store the code and all info. My understanding of datastores is that they are the same within the same game, no matter what server the code executes on.

1 Like

how would the second server get access to its own stuff to display the join code? Because the second server has no knowledge of its own join code, so it can’t grab any data.

Edit: Should messagingservice accomplish this? Like the main server publishes it to the second server’s privateserverid, then the second server picks it up and then we can disconnect it on the main server.

local MessagingService = game:GetService("MessagingService")

-- Make server
local accesscode, psid = TeleportService:ReserveServer(placeid)
local code = math.random(10000, 99999)
-- does everything below if code being generated is not already used 
local GAME_DATA = {
       access = accesscode,
       privateserverid = psid,
       JoinCode = code
}
MessagingService:PublishAsync(psid, GAME_DATA)

-- SECOND SERVER
local connection
connection = MessagingService:SubscribeAsync(privateserverid, function(data)
       data = data.Data -- table of data from the main server
       game.ReplicatedStorage.JoinCode.Value = data["JoinCode"]
       connection:Disconnect()
end)

You could take a look at this resource I open sourced a few months ago:

thank you, but im making this thing where a group of people teleport to a room, then lets say someone gets disconnected from the server. They want to join back, so they use the join code option, then someone (from the second server) tells them the code or they remember.

Only the lobby needs the codes since it is the one that is teleporting the players out. The reserved servers are where the players are being teleported to. If you really wanted to, you could setup a second datastore with the reserved server ID as the key so it would just need to get it’s own game.ReservedServerId value to get it’s own access code and join code. To my knowledge, you cannot get the reserved server access code from within the reserved server. Why? I have no idea. I originally wrote the code as being generated from the reserved server, but that’s kind of pointless without the access code. So I had to rewrite it for the lobby to generate the data instead.

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