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

How to get all instances of game?

Asked by 9 years ago

If you head over to the wiki API for TeleportService you'll see that you can teleport anyone to a specific instance of your game. How can I get all instances of a game?

2
This is rather an excellent question DigitalVeer 1473 — 9y

2 answers

Log in to vote
10
Answered by 9 years ago

How can I get all instances of a game?

What you need to know: Roblox does not have any built-in methods avaliable that will give you a collection of all instances of your games. However, Roblox does have an API method that allows one to obtain the properties of all instances of a place.

Place API

HttpService

Incase you are unsure of what HttpService is, here is some context. When information is sent and/or received from a website, generally they are done through what are called POST or GET requests.

POST will return the response given by a website when you send a particular segment of data.

GET will return a response from a specific URL link.

There are many videos out on youtube and other places that will explain what this feature is more thoroughly than what I can at the moment.

Now you may be aware that Requests can not be sent to any url off of ROBLOX's Domain. This poses the inevitable fact that you will have to use a proxy or your own site to achieve using this particular method of HTTPService.

What I did was, I made my own website off of 000webhost and just quickly added some PHP code which can be found HERE. All the PHP code does is return the exact results that the API from Roblox would return.

In the RbxLua code segments, you will see the link to my site where the PHP is returning the collection of instances. Please note that I did not change the results returned by ROBLOX at all. The code only gets the results and prints them on the screen.

So now, let's see what we can do with this collection that is returned by Roblox. Incase you forgot, here is what the collection table returned by Roblox looks like, [I'll be using a BlackHawk popular game in this case]

Example Of Place ID 23973424 Array

local http = game:GetService'HttpService'
local id = 23973424
local url = "http://digitalrobloxians.comoj.com/API/placeAPI.php?"
url = url.."id=".. id .."&startIndex=0"

function getPlaceJson()
    return http:GetAsync(url)
end

function getDecodedJSON(json)
    return http:JSONDecode(json)
end

function getCollection(decodedJSON)
    return decodedJSON.Collection
end


local jsonOfInstances = getPlaceJson()
local decodedJSON = getDecodedJSON(jsonOfInstances)
local collection = getCollection(decodedJSON)
local numberOfInstances = #collection;

Explanation

If you watched some videos on HttpService or know how it works, much of this should be pretty easy to follow along. All I'm doing is decoding the response, which happens to be a JSON Table, and setting that as a collection. Since collection holds tables for each place instance, just using the length operator on it will return the number of place instances.

This is where things get messy. There are a ton of nested tables inside other nested tables in collection. First, let's start organizing some things.

local instances = {}
for _,v in pairs(collection) do
    table.insert(instances,v)
end

Now we have initialized an array of instances and stored each placeInstance array inside it. Now comes the great part! Inside the arrays we just stored in instances contains a great amount of properties that we can read from. For example, we can get the total number of players in that instance just by creating a function such as this:

function getNumbersOfPlayers(tab)
    return #tab.CurrentPlayers
end

Even better, we can use this on ALL the instances and get the total number of players:

function getTotalPlayers()
local totalNum = 0;
for i,v in pairs(instances) do
    totalNum = totalNum + #v.CurrentPlayers
end
return totalNum;
end

The currentPlayers array returned also contains the properties of each player! We can use this to our advantage. Perhaps you want to make a list of all the players in every single game at the current moment. Doing this is similar to the getTotalPlayers method written above.

function getPlayers()
local allPlayers = {}
for i,v in pairs(instances) do
    for l,b in pairs(v.CurrentPlayers) do
        table.insert(allPlayers,b)
    end
end
return allPlayers;
end

for _,v in pairs(getPlayers()) do
    print(v.Username)
end

The above getPlayers method will return the array of properties of every single player in every single game.


Properties

Here are the properties that can be found under the specific tables/arrays returned.

Collection:

  • Capacity

  • Ping

  • FPS

  • ShowSlowGameMessage

  • Guid

  • PlaceId

  • CurrentPlayers [table]

Players [The tables inside CurrentPlayers]

  • Id

  • Username

  • Thumbnail

  • AssetId

  • AssetHash

  • AssetTypeId

  • Url [link to their profile image]

In The End

Although the following code segment only shows a few things that can be done out of an indefinite amount of things, I hope it gives a general understanding how HTTPService can be used in this case. The end code segment of what we've done should look like the following:

--http://www.roblox.com/games/getgameinstancesjson?placeId=1818&startindex=0

local http = game:GetService'HttpService'
local id = 23973424
local url = "http://digitalrobloxians.comoj.com/API/placeAPI.php?"
url = url.."id=".. id .."&startIndex=0"

function getPlaceJson()
    return http:GetAsync(url)
end

function getDecodedJSON(json)
    return http:JSONDecode(json)
end

function getCollection(decodedJSON)
    return decodedJSON.Collection
end


local jsonOfInstances = getPlaceJson()
local decodedJSON = getDecodedJSON(jsonOfInstances)
local collection = getCollection(decodedJSON)
local numberOfInstances = #collection;


local instances = {}
for _,v in pairs(collection) do
    table.insert(instances,v)
end


function getNumbersOfPlayers(tab)
    return #tab.CurrentPlayers
end

function getTotalPlayers()
local totalNum = 0;
for i,v in pairs(instances) do
    totalNum = totalNum + #v.CurrentPlayers
end
return totalNum;
end

function getNumbersOfPlayers(tab)
    return #tab.CurrentPlayers
end

function getPlayers()
local allPlayers = {}
for i,v in pairs(instances) do
    for l,b in pairs(v.CurrentPlayers) do
        table.insert(allPlayers,b)
    end
end
return allPlayers;
end

Useful Links

HttpService: http://wiki.roblox.com/index.php?title=API:Class/HttpService

JSONDecode: http://wiki.roblox.com/index.php?title=API:Class/HttpService/JSONDecode

GetAsync: http://wiki.roblox.com/index.php?title=API:Class/HttpService/GetAsync

PostAsync: http://wiki.roblox.com/index.php?title=API:Class/HttpService/PostAsync

API's: https://github.com/matthewdean/roblox-web-apis

0
This only covers up to the first page of game instances. I'll edit it soon to show how to change which pages you particularly want DigitalVeer 1473 — 9y
2
Thank you. I was trying to avoid using HTTP service because I am still learning PHP, but this made my job much easier, thank you for the detailed explanation, and the API links are very useful! devSparkle 84 — 9y
Ad
Log in to vote
-4
Answered by 9 years ago

Try it yourself first, then give us your error if you get one.

2
I didn't find any documentation on how to get it, that's why I asked. devSparkle 84 — 9y
1
I'll upvote this so you don't loose much reputation, but let this serve as a lesson for future posts. DigitalVeer 1473 — 9y

Answer this question