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?
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.
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;
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.
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]
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
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
Try it yourself first, then give us your error if you get one.