Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get all servers for a game?

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:

local URL = ("https://www.roblox.com/games/getgameinstancesjson?placeId="..game.PlaceId.."&startindex=")
local a = game:HttpGet(URL.."0")
local b = game:GetService("HttpService"):JSONDecode(a)
local function ServerHop()
    for i,v in pairs(b.Collection) do
       if v.UserCanJoin == true then
            game:GetService("TeleportService"):TeleportToPlaceInstance(game.PlaceId, v.Guid)
        end
    end
end

ServerHop()

Script 2:

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

ServerHop()

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 :)

2 comments

0

I would use MessagingService to do that

0

I dont know how to use messagingservice lol

1 answer

✓ Accepted answer

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

local HttpService = game:GetService("HttpService")

local 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

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

The response body (servers) would look something like

{
  "previousPageCursor": null,
  "nextPageCursor": null,
  "data": [
    {
      "id": "c89debe7-432e-47a3-8e5e-694c848b96d1",
      "maxPlayers": 100,
      "playing": 6,
      "playerIds": [
        348069207,
        1786509767,
        1641588628,
        1737102487,
        1761545992,
        1073629272
      ],
      "fps": 59.993629,
      "ping": 118
    }
  ]
}

, 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.

1 comment

0

Thanks! I didn't know about the cursor thing.

This discussion is preserved as it was. New answers, comments, and votes are closed.