Global Players Count

Hello, I’m PLX, currently making a Roblox game.

I’m looking for a global players counter, explanation: I wan’t want this to happen on the game place, it displays how many players are playing in that place

So could someone tell me how to achieve this?

You’ll want to use the Games Web API in order to get the number of players playing in a game.

You’ll just need to use HttpService to send a request, and then the api endpoint used will return the number of players currently playing the game. (Note: HttpService doesn’t allow direct Http requests to the Roblox Web API, so you’ll need to use a proxy :slight_smile: )

local HttpService = game:GetService("HttpService")

local function getConcurrentPlayers(universeId: number)
   local data

   local success, errorMessage = pcall(function() -- Always use a pcall when sending http requests
   data = HttpService:GetAsync(`https://games.roblox.com/v1/games?universeIds={universeId}`) -- Remember to replace the endpoint with a proxy equivalent, or else it wont work
end)

   if success then
      local gameData = HttpService:JSONDecode(data)

      local playerCount: number = gameData[0].playing

      return playerCount
else 
  warn(`Error when attempting to get Game Data: {errorMessage}`)
end

In the snippet above, we send a Http GET Request to this endpoint:

https://games.roblox.com/v1/games

to get the game data of the provided universeId query.

When a valid Universe ID is provided, the endpoint will return this json response:

Expand
{
  "data": [
    {
      "id": 0,
      "rootPlaceId": 0,
      "name": "string",
      "description": "string",
      "sourceName": "string",
      "sourceDescription": "string",
      "creator": {
        "id": 0,
        "name": "string",
        "type": "string",
        "isRNVAccount": true,
        "hasVerifiedBadge": true
      },
      "price": 0,
      "allowedGearGenres": [
        "string"
      ],
      "allowedGearCategories": [
        "string"
      ],
      "isGenreEnforced": true,
      "copyingAllowed": true,
      "playing": 0,
      "visits": 0,
      "maxPlayers": 0,
      "created": "2024-04-30T16:30:49.188Z",
      "updated": "2024-04-30T16:30:49.188Z",
      "studioAccessToApisAllowed": true,
      "createVipServersAllowed": true,
      "universeAvatarType": 1,
      "genre": "string",
      "isAllGenre": true,
      "isFavoritedByUser": true,
      "favoritedCount": 0
    }
  ]
}

We’d then get the playing property inside the json response, in which the value of the playing property is the amount of players currently playing the game.

I hope this helped :slight_smile:

1 Like

hello! Thank you for helping me, does it work on textlabels?

if u use the prev example from @valientWind u can convert the return playercount what is a number to a string with tostring()
ATextLabel.Text=tostring(playerCount)
1 Like