Help with ordered Datastore - Roblox Scripting Support

Hi, so I have a script that teleports a player to a place and sends an extra piece of information to that server. This is a random number. Now, when the player joins that world. I need to somehow make a global/ordered datastore with the code and the JobId of that world. How would I go about this?

Hey there,

Why specifically does the JobId have to be random? Why can you not use a global variable of sorts that lets you pull from a datastore as a sort of key while at the same time using the players UserId? Can you elaborate further?

JobId is huge, and so Iā€™m passing a smaller, randomly generated code to that private server, then the private server will store the code as the key and the JobId as the value of the ordered-datastore so that the lobby server can get that code from the cloud when a player enters it and get the value of that code and teleport the player to the server using the JobId linked to the code.

Diagram to hopefully explain what I need better:

I just need to know the basics of ordered datastore. But ever tutorial I search up is how to make a top 10 leader board, where I just want to save 1 datastore to the cloud to potentially be accessed if a player tries to access it by putting in a code. Just like a normal datastore, saving values to the player, but I want to save values to the cloud not under the player.

So basically youā€™re trying to make a cloud system to save other players code, and then a way to potentially load it back for them through a datastore, is what Iā€™m understanding?

Basically, I would like everybody playing the ā€œLobby placeā€ to put in a code and the the game checks to see if there is a ā€˜globalā€™ data store with that inputted code as the key of that datastore. I say a cloud system as ordered datastores arenā€™t linked to players, itā€™s basically datastore but a cloud, most people use them for public leaderboards.

Oh, I think I understand! So, to grasp or find a list of players by requesting a custom key to the datastore that players can view? :thinking:

Before I try to help, Iā€™d like to grasp exactly what it is youā€™re trying to do, then I can help brainstorm with you / list solutions I think would work! :wink:

Thanks, so ordered datastores have a ā€œkeyā€ (a string) as a first value:

	OrderedDataStore:SetAsync(code, game.JobId) 

code = the key
game.JobId = the value

So, Iā€™d save that to the cloud (Cause itā€™s an ordered datastore, they save to a ā€˜publicā€™ key I guess (Which is the first value)
Then in a different place Iā€™d like players to be able to enter a the key for the saved datastore and then the game takes that input and seeā€™s if there is any data saved under that key.

I really appreciate your help as this is a very confusing topic.

No problem, I personally would like to understand this tooā€¦ so based on what youā€™re saying, youā€™d basically want to create a local datastore for that server, completely personal to that server only, but the problem is that the JobId is too long, so you canā€™t really use it, and youā€™re trying to find a shortcut?

Iā€™d like to do the opposite lol, so Iā€™d like to create a public datastore for 1 game (1 game can have multiple places) as shown here:

btw ā€œIā€™m freeee!!ā€ is the lobby world:

Oh! I understand, youā€™re creating LOBBY codes that players can join off of if given they type a valid private code? That makes so much more sense!

Let me know if thatā€™s what youā€™re trying to do, because then I can actually start to help!

And, sorry for having you explain this extensively, I just want to know primarily so I can help 100% :happy3:

Yeah! A private server system basically. But very basic, so when you create a server you will be teleported to a ā€œreserved serverā€ which is a server reserved for the player (Just like a private server), Iā€™m passing a value to that reserved server which is a randomly generated 6 digit number. When the player teleports to that reserved server, the reserved server publishes that 6 digit number as the key of the ordered datastore (as I explained earlier) and the value would be the game.JobId which is a huge string of numbers that lets the player teleport to that server instance. What Iā€™m basically doing is linking a short, randomly generated 6 digit number to that jobId and publishing it to the game cloud so when people enter that 6 digit code, the game seeā€™s that itā€™s linked to a jobId and then teleports them to that reserved server. Iā€™m so sorry for the extremely long paragraph, itā€™s just really hard to explain in a short paragraph lol. Thanks again for your help so far.

Well, hereā€™s my first solution(?):

Can you in any way, shape or form save the JobId at all? Maybe you could store it in a table with another code being a specific ā€˜randomā€™ index in which you can pull from as that being the code, in reality it loads the actual JobId from that code stored in that index?

Below is the image of what Iā€™ve managed to get it to do, it gives the randomly generated code and below is the JobId that roblox can use to teleport people to the server. I just need to publish it using ordered datastore.

And of course, while generating indexes, youā€™d have to make sure that itā€™s also recursive so that it never generates the same sort of code for different private servers. Would that work by any chance?

Here is the code I have so far:
Lobby game:

local ts = game:GetService("TeleportService")
local DataStoreService = game:GetService("DataStoreService")
local PointsODS = DataStoreService:GetOrderedDataStore("PrivateServerCodes") 

game:GetService("ReplicatedStorage").StartPrivateGame.OnServerEvent:Connect(function(player, code1, placeid)

	local code = ts:ReserveServer(placeid) -- Returns a code
	ts:TeleportToPrivateServer(placeid,code,{player}, "SpawnLocation", code1)
end)



local tp = game:GetService("TeleportService")
local players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local PointsODS = DataStoreService:GetOrderedDataStore("PrivateServerCodes") 

game:GetService("ReplicatedStorage").JoinPrivateGame.OnServerEvent:Connect(function(player, code1, placeid)
	local function printTopTenPlayers()
		local isAscending = false
		local pageSize = 90
		local pages = PointsODS:GetSortedAsync(isAscending, pageSize)
		local topTen = pages:GetCurrentPage()

		-- The data in 'topTen' is stored with the index being the index on the page
		-- For each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
		for rank, data in ipairs(topTen) do
			local name = data.key
			local points = data.value
			print(name .. " Is the code, and  " .. points .. " Is the JobId ")
			ts:TeleportToPlaceInstance(placeid, points, player)
		end

		-- Potentially load the next page...
		--pages:AdvanceToNextPageAsync()
	end

	-- Get some data
	PointsODS:GetAsync(code1)

	-- Display the top ten players
	printTopTenPlayers()
	
	
end)

ReservedServer Place:

local tp = game:GetService("TeleportService")
local players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local PointsODS = DataStoreService:GetOrderedDataStore("PrivateServerCodes") 

game:GetService('ReplicatedStorage').PublishServerCode.OnServerEvent:Connect(function(player, code, Jobid)
	local function printTopTenPlayers()
		local isAscending = false
		local pageSize = 90
		local pages = PointsODS:GetSortedAsync(isAscending, pageSize)
		local topTen = pages:GetCurrentPage()

		-- The data in 'topTen' is stored with the index being the index on the page
		-- For each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
		for rank, data in ipairs(topTen) do
			local name = data.key
			local points = data.value
			print(name .. " Is the code, and  " .. points .. " Is the JobId ")
		end

		-- Potentially load the next page...
		--pages:AdvanceToNextPageAsync()
	end

	-- Create some data
	PointsODS:SetAsync(code, Jobid)

	-- Display the top ten players
	printTopTenPlayers()
end)

For instance, this is what I mean:

local Lobbies = {};

local function GenerateRecursive()
-- blah whatever u want to generate code here, make sure it is recursive though!
end

function Lobbies:CreatePrivateLobby(self)
       local Code = GenerateRecursive();
       Lobbies[Code] = self.JobId;
end

-- Somehow tie this into the new worlds JobId, call the exact world then run the private lobby
-- function with it?
Lobbies:CreatePrivateLobby();

Then, when it comes time to actually getting the code, check for if any of the indexes are matched with the players entries, and if so, pull that JobId from it?

1 Like

Hmmm, I have never really done any scripting with self or recursive numbers or indexes.