Getting current player count using WebAPI and HTTP Service?

Hello, everyone!

I’m currently trying to figure out how to create an in-game player counter that would update every 5 seconds or so. My idea is to basically obtain the Game’s player count that’s displayed on the game’s page. So if it says there’s 200 players online, my menu will say that as well.

I want to expand this to including playlists as well (which would basically just be taking 2 or more places and combining the player counts of both.)

If anyone has any ideas on how I could achieve this, please let me know! :slight_smile:

Thanks,
TheRings0fSaturn

9 Likes

I am able to make it so that it shows how many people are in a single server. Although I am not sure how
to do you are wanting to do. So if you want it to say how much people are in a single server I can do that but not the total count.

4 Likes

Isn’t it through PlaceID and then finding the player count variable through web API?
If that’s the case, you could potentially combine all of the results from each place with a universe.

But yeah, a single place for now would be great.

3 Likes

You could possibly achieve this by creating a web server and having that web server look at the page’s player count by looking at the HTML of the Roblox game page. When the web server receives a get request, it takes a look of it and sends back a response. The Roblox game sends the get request every few seconds or such.
You cannot do this through a script as it fails a trust check.

5 Likes

Another thing I am wondering is would the counter be a gui or what?

4 Likes

Yes, it would be within the start place where the main menu would be.

It would basically say “25 players online.”

Something like this:

2 Likes

Not sure how to get an accurate player count but there is a way to get the player count on the website (I’m sure you’ve thought of this and have already ruled it off as inefficient but hey it’s a way).

You could use a proxy and send a GET request to your game page then parse out the playing thing.

Very inefficient but it is a way.

2 Likes

I believe you can use DataStoreService for this. Just increment it up by 1 when a player joins and subtract 1 when a player leaves.

1 Like

https://games.roblox.com/docs#!/Games/get_v1_games_placeId_servers_serverType

This might be useful for getting the current active servers on a place, but I don’t think there is current player count.

I’m sure this is a WIP by Roblox.

1 Like

I don’t really think this could work because of DataStore limits. Plus, DataStore data is wiped with any change made to it. :man_shrugging:

1 Like

What do you mean it is wiped? Also, I store the value on the “local” server then update the datastore every 30 seconds or so to prevent any limits.

1 Like

Actually screw parsing HTML, use this endpoint and it will return an array of places created by the user:

https://www.roblox.com/users/profile/playergames-json?userId=ID

One of the items in the game object is PlayerCount, which will get you the player count for the place.

(This is for getting the player count of a place still not a universe I think :confused: )

5 Likes

Like, if you try to have a datastore for a server list, any item(s) you have will wipe any previous data.

I.e.

Before updating:

Server 1
Server 2
Server 3

After updating:

Server 1

This actually appears to be what I’m looking for.

I intend on supplying the API endpoint with a different PlaceID for each place, and then taking the player count from each place and combining them, giving me the “total” player count.

Same applies for each playlist.

Edit: Is there a version of this available for groups?

1 Like

Do this (I’m on mobile so excuse any mistakes):

local CountStore = game:GetService("DataStoreService"):GetDataStore("PlayerCount")
local Count = 0

game:GetService("Players").PlayerAdded:Connect(function(Player)
    Count = Count + 1
end)
game:GetService("Players").PlayerRemoving:Connect(function(Player)
    Count = Count - 1
end)

while wait(30) do
    CountStore:UpdateAsync(PlaceId, function(Old)
        if Old then
            return Old + Count
        else
            return Count
        end
    end)
end

I may have missed a few bits but it should get the idea across. It’s also 5AM and I’ve been up for over 48 hours now.

2 Likes

Although this may not be completely what you are looking for. I made a local script that checks for counts all the people in the server.

local PlayerCount = 0

game.Players.PlayerAdded:Connect(function()

PlayerCount = PlayerCount +1

end)

game.Players.PlayerRemoving:Connect(function()

PlayerCount = PlayerCount -1

end)

script.Parent.Text = (PlayerCount .. "Players Online")
1 Like

Glad to have helped in a way.

There is a version of this for groups!

https://groups.roblox.com/v1/groups/{GroupID} ← This one to get a specific group via ID. This will get you all the info on a group (Including MemberCount)
http://api.roblox.com/users/{UserID}/groups ← Get a list of groups the player is in

Hope this helped! :slight_smile:

2 Likes

A way to get a specific game in a group though?

1 Like

Also, I haven’t really done much with HTTP stuff.

If I mange to get a specific place, how would I obtain just the player count?

1 Like

Ok so you wanted an api for getting the group’s place. Sorry - misunderstood.

I actually was blissfully unaware that there was a master api for games.

https://games.roblox.com/v1/games?universeIds={UniverseIDs} ← This will allow you to get a game by UniverseIDs (it does allow multiple). It will return an array containing each universe with the respective player counts.

The data returned should look like this
{
  "data": [
    {
      "id": 0,
      "rootPlaceId": 0,
      "name": "string",
      "description": "string",
      "creator": {
        "id": 0,
        "name": "string",
        "type": "string"
      },
      "price": 0,
      "isExperimental": true,
      "allowedGearGenres": [
        "string"
      ],
      "allowedGearCategories": [
        "string"
      ],
      "playing": 0,
      "visits": 0,
      "maxPlayers": 0,
      "created": "2019-03-03T04:51:24.149Z",
      "updated": "2019-03-03T04:51:24.150Z",
      "studioAccessToApisAllowed": true,
      "universeAvatarType": "MorphToR6",
      "genre": "string"
    }
  ]
}
7 Likes