Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get all servers for a game?

Asked by 4 years ago
Edited by Leamir 4 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

So I’m making a game and I want to teleport the players to another server. I can kind of do this but it get an error message if i do it like 5 times in a row.

I have tried 2 different ways of doing it but they still have the same problem

Script 1:

01local URL = ("https://www.roblox.com/games/getgameinstancesjson?placeId="..game.PlaceId.."&startindex=")
02local a = game:HttpGet(URL.."0")
03local b = game:GetService("HttpService"):JSONDecode(a)
04local function ServerHop()
05    for i,v in pairs(b.Collection) do
06       if v.UserCanJoin == true then
07            game:GetService("TeleportService"):TeleportToPlaceInstance(game.PlaceId, v.Guid)
08        end
09    end
10end
11 
12ServerHop()

Script 2:

01local servers = game:GetService("HttpService"):JSONDecode(game:HttpGet("https://games.roblox.com/v1/games/".. game.PlaceId.. "/servers/Public?sortOrder=Asc&limit=100"))
02local function ServerHop()
03    for i,v in pairs(servers.data) do
04        if v.playing ~= v.maxPlayers and v.playerIds ~= game:GetService("Players").LocalPlayer.UserId then
05            game:GetService("TeleportService"):TeleportToPlaceInstance(game.PlaceId, v.id)
06        end
07    end
08end
09 
10ServerHop()

Is there any way to get all the servers of a game, not just 100 (or some number that isn’t all of them)? If there is an api link for that or something that would really help :)

0
I would use MessagingService to do that Leamir 3138 — 4y
0
I dont know how to use messagingservice lol TowstedShoes 2 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

An easy way to get a game's server list is using the Games Api. An example for this would be

1local HttpService = game:GetService("HttpService")
2 
3local servers = HttpService:JSONDecode(HttpService:GetAsync("https://games.roblox.com/v1/games/4483381587/servers/Public?sortOrder=Asc&limit=100"))

servers would now be a dictionary of place info. Accessing the list of servers would be as easy as

1for _, server in pairs(servers.data) do
2    print("Server Id: " .. server.id)
3end

The response body (servers) would look something like

01{
02  "previousPageCursor": null,
03  "nextPageCursor": null,
04  "data": [
05    {
06      "id": "c89debe7-432e-47a3-8e5e-694c848b96d1",
07      "maxPlayers": 100,
08      "playing": 6,
09      "playerIds": [
10        348069207,
11        1786509767,
12        1641588628,
13        1737102487,
14        1761545992,
15        1073629272
View all 21 lines...

, and if you wanted to get the next page, simply add '&cursor=' and the nextPageCursor of that response body to the end.

If you found this answer helpful, do mark it as one.

0
Thanks! I didn't know about the cursor thing. TowstedShoes 2 — 4y
Ad

Answer this question