Best Way to Get Random PlaceIds From a Game

Recently I’ve been thinking about creating joinable servers from UI, meaning that I would like a better way of fetching let’s say, 10 servers by PlaceId.

The way I am currently using is to save the PlaceIds in a Datastore, which then I would load onto a ServerScript via OrderedDatastore. But, I need to get random servers each time, so I would have to use AdvanceToNextPageAsync about 100 times every time the player want’s to reload the servers(this is done so that servers that have a higher PlaceId and a lower PlaceId would have a chance to be picked).

If it’s still confusing, here’s an example:

function ServersModule.GetServers() -- Fires every time the player reloads the server list
	local Pages = OrderedServer:GetSortedAsync(false, 10)
	for i = 0, math.random(0, 100) do
		Pages:AdvanceToNextPageAsync() -- Goes to the next page a random amount of times
	end
	return Pages:GetCurrentPage() -- Return the page
end

As you can see, this is very inefficient, is there a better way to go around this? Is there a function that I don’t know about that can do this kind of thing? I’m fetching the PlaceIds so I can teleport the players to that specific server.

All help is appreciated,
Thanks!

If you just want a list of Places in a Game, you can use the AssetService:

However, I think you’re referring to reserved servers? In which case, you could use the MessagingService to kind of “ping” active servers. Just a suggestion.

1 Like

Thanks for the suggestion! Although there isn’t a way of trying it out yet.

Some questions I want to ask about this:

  1. Does it return a random PlaceId?
  2. Off-Topic: Messaging service can message specific servers, assuming you have the Id, correct?

Afaik, MessagingService literally just broadcasts to all servers. You’d have to handle JobIds yourself.

You could have it broadcast a message which has all servers respond, store the IDs in a table, and pick one at random. Obviously there’s no way to know when all servers have responded, so you’d just need a timeout that you’d expect all servers to respond within.

The only other way to know for sure which servers are active would be the way you’re currently handling it, or via your own external web server (possibly a Node server with an SQLite database, or in-memory if you can guarantee your external server won’t shutdown).

1 Like