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:
01 | local URL = ( "https://www.roblox.com/games/getgameinstancesjson?placeId=" ..game.PlaceId.. "&startindex=" ) |
02 | local a = game:HttpGet(URL.. "0" ) |
03 | local b = game:GetService( "HttpService" ):JSONDecode(a) |
04 | local 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 |
10 | end |
11 |
12 | ServerHop() |
Script 2:
01 | local servers = game:GetService( "HttpService" ):JSONDecode(game:HttpGet( "https://games.roblox.com/v1/games/" .. game.PlaceId.. "/servers/Public?sortOrder=Asc&limit=100" )) |
02 | local 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 |
08 | end |
09 |
10 | 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 :)
An easy way to get a game's server list is using the Games Api. An example for this would be
1 | local HttpService = game:GetService( "HttpService" ) |
2 |
3 | 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
1 | for _, server in pairs (servers.data) do |
2 | print ( "Server Id: " .. server.id) |
3 | end |
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 |
, 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.