Getting current player count using WebAPI and HTTP Service?

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"
    }
  ]
}
6 Likes

Assuming you want to know how to process the data it’s pretty simple.
The data is stored in a JSON format (but it will return a table with HttpService:JSONDecode)

Here’s accessing the player count of the example:

Eample
local body = game:GetService("HttpService"):JSONDecode(res) --res would be the response from the server

local playerCount = body.data[1].playing
print(playerCount)

Expected Output: 0

(Havent tested, just typed it out here might be some complications…)

8 Likes

Thank you very much for your help! :slight_smile:

Would like to thank everyone else who helped too!

1 Like

What is 'res???

2 Likes

What do i do with “res”???

i keep getting this error
image

1 Like

Plus i’ve put my Test game URL and i got this error
image

(Sorry for the late reply // necrobump)

res is the response from the server – it’s the result of a GET request. Here’s an example:

local url = "https://games.roblox.com/v1/games?universeIds={UniverseIDs}"

local res = game:GetService("HttpService"):GetAsync(URL) 

-- Then paste the other code

This obviously won’t work because:

  1. You need the universe ID
  2. You need to proxy the request since Roblox does not allow you to use Roblox’s web apis from the game server.

However, this is the basics of how it works

2 Likes

So, in that case, how would the script be?
I just need to change the URL to my game URL or i need to do more things?

sorry i’m a bit bad at this topic :sweat_smile:

technically no, all that needs changing is the URL and pasting the rest of the code

but you need to 1 past the ID of the universe (NOT THE PLACE ID) and then proxy it (you can do it yourself – there are many tutorials in #resources for it).

2 Likes

1 year late but i got this

1 Like

replace games.roblox.com with games.roproxy.com or another proxy

1 Like

Does not work either, it gives me that bad request error.
image
The code:

Turn on HttpService permission in your game Settings. 3 years later…