Hey, so i was just wondering this to improve my stats of my game!
So ya'll know games like "tower battles" and "Tower Defense Simulator" right? if you do, then what i'm trying to figure out is how they connect the games of the teleport-er maps into the main lobby?
Lets say this as an example: A person plays a game in Tower Defense Simulator but there is no 1 in the lobby, and it shows 1 person is playing it. how do you connect that teleport-ed game to the lobby from the players?
I really need to understand this please, Thank you.
Links: Profile: https://web.roblox.com/users/202371881/profile Game: https://web.roblox.com/games/5149487599/NEW-GAMEMODE-TACTION Group: https://web.roblox.com/groups/6607132/BlueJ-Studios#!/about
Using the official Roblox Api, you can get the number of players in several different places.
For example, if I wanted to check how many people were playing crossroads (place id 1818), I would go to the link https://www.roblox.com/games/getgameinstancesjson?placeId=1818&startindex=0. The text it shows is:
{ "PlaceId":1818, "ShowShutdownAllButton":false, "Collection":[ { "Capacity":8, "Ping":66, "Fps":59.974594, "ShowSlowGameMessage":false, "Guid":"8ad4c4b2-5c00-41f7-92f9-747b60382baa", "PlaceId":1818, "CurrentPlayers":[ { "Id":0, "Username":"A Roblox Player", "Thumbnail":{ "AssetId":0, "AssetHash":null, "AssetTypeId":0, "Url":"https://tr.rbxcdn.com/0f0a14eb846140dfc1f28870921b38d8/48/48/AvatarHeadshot/Png", "IsFinal":true } }, ... ], "UserCanJoin":true, "ShowShutdownButton":false, "JoinScript":"Roblox.GameLauncher.joinGameInstance(1818, \"8ad4c4b2-5c00-41f7-92f9-747b60382baa\")", "FriendsDescription":"", "FriendsMouseover":"", "PlayersCapacity":"7 of 8 players max" }, ... ], "TotalCollectionSize":3 }
To go to the link with Roblox scripts, you can use
local HttpService = game:GetService("HttpService") local PlaceId = 1818 -- Crossroads local StartIndex = 0 local Link = string.format("https://www.rprxy.xyz/games/getgameinstancesjson?placeId=%s&startindex=%s", PlaceId, StartIndex) local Data = HttpService:GetAsync(Link) print(Data)
It will print the data in the output. But let's say that you want to turn it into a Lua table. You would, of course, use HttpService:JSONDecode
. Let's add that to our script.
local HttpService = game:GetService("HttpService") local PlaceId = 1818 -- Crossroads place id local StartIndex = 0 -- Start at server #0 local Link = string.format("https://www.roblox.com/games/getgameinstancesjson?placeId=%s&startindex=%s", PlaceId, StartIndex) -- Set link with parameters local Data = HttpService:GetAsync(Link) -- Get data from link local DecodedData = HttpService:JSONDecode(Data) -- Decode the data -- print(Data)
Count the number of people in the servers
local HttpService = game:GetService("HttpService") local PlaceId = 1818 -- Crossroads place id local StartIndex = 0 -- Start at server #0 local Link = string.format("https://www.roblox.com/games/getgameinstancesjson?placeId=%s&startindex=%s", PlaceId, StartIndex) -- Set link with parameters local Data = HttpService:GetAsync(Link) -- Get data from link local DecodedData = HttpService:JSONDecode(Data) -- Decode the data local PlayerCount = 0 for Index, Server in pairs(DecodedData["Collection"]) do PlayerCount += #Server["CurrentPlayers"] end
Notice this only returns the first ten servers. This problem can be easily solved by integrating a loop.
local HttpService = game:GetService("HttpService") local PlaceId = 1818 -- Crossroads place id local StartIndex = 0 -- Start at server #0 local Link = string.format("https://www.roblox.com/games/getgameinstancesjson?placeId=%s&startindex=%s", PlaceId, StartIndex) -- Set link with parameters local Data = HttpService:GetAsync(Link) -- Get data from link local DecodedData = HttpService:JSONDecode(Data) -- Decode the data for Index, Server in pairs(DecodedData["Collection"]) do PlayerCount += #Server["CurrentPlayers"] end for NewStartIndex = 10, DecodedData["TotalCollectionSize"], 10 do Link = string.format("https://www.roblox.com/games/getgameinstancesjson?placeId=%s&startindex=%s", PlaceId, NewStartIndex) local NewData = HttpService:GetAsync(Link) local NewDecodedData = HttpService:JSONDecode(NewData) for Index, Server in pairs(NewDecodedData["Collection"]) do PlayerCount += #Server["CurrentPlayers"] end end print(PlayerCount)
Hope this works!