How do I check and loop through all my active reserved sessions?

So I’ve decided I want to create a main menu for my game and allow for players to join different kind of sessions (such as solo, roleplay based or public)

My code:

local DatastoreService = game:GetService('DataStoreService')
local ServersDatastore = DatastoreService:GetDataStore('Servers')

function TeleportToDedicatedSession(Player, Type, Id)
	local message = Instance.new('Message', workspace) 
	message.Text = "Teleporting"	
	
	repeat wait(1)
		local success, err = pcall(function()
			local players = {Player}
			return TeleportService:TeleportToPrivateServer(OttawaId, Id, players)
		end)
	until success or not Player
end

function CreateNewSessionAndTeleport(Player, Type)
	local code = TeleportService:ReserveServer(OttawaId)
	local message = Instance.new('Message', workspace) 
	message.Text = "Teleporting"

	repeat wait(1)
		local success, err = pcall(function()
			local players = {Player}
			return TeleportService:TeleportToPrivateServer(OttawaId, code, players)
		end)
	until success or not Player
	
	local serverDetails = {id = code, host = Player.UserId, type = Type}
	ServersDatastore:SetAsync(code, serverDetails)
end

My main questions are:

  • When I create a new session and all the players leave, how do I check the number of players that are in that server? (without creating place to place communication and doing it in the one script I’m currently writing in)

  • How can I loop through all active sessions that I’ve saved to a datastore?

I’m really curious as to what I can do here, any advice would be perfect. Sorry if the thread sounded a bit bonkers got a bit confused halfway writing it.

Thanks


EDIT:
For an example of what I’m attempting to achieve:
image

  • What you can do is have the server that the players teleport to call SetAsync() once all players load in, then have the value be the number of players currently in that server (as well as your other information such as the code, host, etc). Then from the script that teleported the players just call GetAsync() to retrieve the information (make sure to add a wait to let the other server set up). You’ll already know what the key is since you’re calling it from that script.

  • There’s no possible way at the moment to iterate through a datastore and grab all the keys. You would have to set one single key that contains all the information of all active sessions and then retrieve that key, but it can get messy. A more preferred option would be to use OrderedDatastores, which is what most people mainly use when creating server lists.

A little side note: It’s better to set the new server information after the players have teleported to it. That way you don’t set keys with non-existent servers in case all players failed to teleport or Roblox failed to create the server for whatever reason.

2 Likes

Then from the script that teleported the players just call GetAsync() to retrieve the information (make sure to add a wait to let the other server set up).

I’m kind of confused on how I get the ReservedServer code to return to the datastore. You stated you’ll already know however I’m not sure how my other script will know. Could you provide me a small example of what you mean?

Same thing with the type value, I don’t know how I’ll check from the other game what game-mode I’ve put people to.

Sorry I think I might’ve misunderstood your first question.

When I create a new session and all the players leave, how do I check the number of players that are in that server?

By server are you referring to the one that the players just left from or the new server that is created?

Oof, that’s painful.

You can use my service, RBXMod, to run a module that all your games can connect to and share data. It would be as simple as making a table in the RBXMod and using the server ID as the key and the number of players as the value. You can iterate all you want through that, search it, or perform other operations and add additional data if you wanted to. Interested? If so, I’ll work with you right now to set it up.

https://rbxmod.com/about.html

2 Likes

Yea I forgot to mention you can also use a third party service to make life easier and achieve that. Just saying Roblox specifically doesn’t provide that option.

1 Like

I want to know when:

  • A players leave the game
  • A player joins the game

I’ll be in contact, thank you.

Yes I know about that but I need to know how I’m going to update that through the main game to the main screen game. Also, I need to know the CodeId that was used to teleport the players in the main game so I can update the datastore. (ill use the datastore to tell the other server about the new/old players i guess)

What you’re trying to achieve is fairly messy because of the limitations of Datastores. Also, MessagingService is probably going to be coming out soon-ish (a service that allows developers to communicate between servers). So if you really want this feature now you should create some kind of interface (module, service singleton) and look into highly abstracting it.

It’s good to be mindful of the limitations of Datastores while you’re planning this out. The TL;DR most relevant to it is that Datastores has something called budgeting. This means you can’t set or get too often or else ROBLOX will automatically throttle (delay) your requests to the Datastore. Additionally, OrderedDataStores has a maximum page size of 100 entries.

The basic process is to use OrderedDataStores to insert entries of server information. The key would be the PrivateServerId you get from ReserveServer and the value could be anything you want. Ideally, it should be something like tick() so that the most recently updated servers reach the top (since OrderedDataStores will automatically sort by the highest value in each entry).

Then, in a separate Datastore, you store the actual information of the server in a big table under the key of the PrivateServerId. Thus, the server would poll servers using GetSortedAsync, take each entry’s key and try to find the actual information in a normal Datastore under the same key.

Anytime you want to update the server information within the server, read game.PrivateServerId, find its information Datastore, and UpdateAsync accordingly. You can update the entry in OrderedDataStore too if you want to sort by whatever filter you want (most players, most recently updated, etc).

So long as you’re not constantly refreshing the servers and you have a sane amount (<100), you should be fine in terms of throttling. Just keep in mind that the amount of GetAsync’s you are doing can get a little high (assuming 60 servers, 60 GetAsync’s). So if you’re doing Datastores anyplace else, remember to take that into account. When in doubt you can always check the limitation page’s budget consumption chapter linked to above.

When I create a new session and all the players leave, how do I check the number of players that are in that server? (without creating place to place communication and doing it in the one script I’m currently writing in)

You can’t. ROBLOX currently has no asynchronous calls that will find these automatically for you.

How can I loop through all active sessions that I’ve saved to a datastore?

Use OrderedDataStores for quickly finding entries with another Datastore for accessing its information.

1 Like

Thank you for the detailed response.

I took it down for a minute. It is back now. BTW, it only works with Firefox right now.

1 Like