How do I get the amount of players playing my game?

I would like to know how I would get the amount of players playing my game.

Yes, I have searched on google and nothing helped me. I am kinda new to scripting and all this HTTP stuff makes me feel like “What’s that” or “What and how do I use that?”.

I don’t know how to use a proxy so basically I am request for a script that’s pre-made for me.

But again, I really am new to roblox coding. Most of my games use free models, pre-made GUIS (from toolbox) and pre-made scripts (from toolbox) that I have tampered with only 0.10%

So if you could help me out here then thanks. :smiley:

9 Likes

You should be able to just define a variable and use a for loop, then update that for loop everytime a player is found.

local plrcount = 0 -- Define the number at first.

for i,v in pairs(game.Players:GetChildren()) do
if v:IsA("Player") then -- Check if it's a player.
       plrcount = plrcount+1 -- Update the player count.
    end
end

I’d also recommend running this when a player is added:

local plrcount = 0 -- Define the number at first.

game.Players.PlayerAdded:Connect(function()
for i,v in pairs(game.Players:GetChildren()) do
if v:IsA("Player") then -- Check if it's a player.
       plrcount = plrcount+1 -- Update the player count.
    end
end
end)

If you want to get the players in all servers, then use messagingservice.

local plrcount = 0
local ms = game:GetService("MessagingService")
ms:SubscribeAsync("PLRCOUNT",function(message)
	plrcount = message.data
end)

game.Players.PlayerAdded:Connect(function()
    plrcount = plrcount+1
    ms:PublishAsync("PLRCOUNT",plrcount)
end)
game.Players.PlayerRemoving:Connect(function()
    plrcount = plrcount-1
    ms:PublishAsync("PLRCOUNT",plrcount)
end)

I haven’t tested this yet, but you can ask any questions you’d like. I haven’t used MessagingService in a while.

5 Likes

If you’re asking to get all of the players in all servers, HTTP requests are probably the best you can use but there are some other alternatives - you can use DataStores or MessagingService.

If that’s not what you asked for, the other posts will help you

I would also like to point out that we do not write scripts for you in the Scripting Support category.

You can fetch the player count using the # operator on Players:GetPlayers() (which returns the length of arrays)

local PlayerCount = #Players:GetPlayers()
5 Likes

I think he means how many players are currently playing his game in all servers.

Also, you can simply do instead:

print(#game.Players:GetChildren())

or

print(#game.Players:GetPlayers())
1 Like

Best way is to use GetPlayers() which returns an array contains player instances. You can use the “#” operator to get the length of that array.

local numOfPlayers = #game:GetService(“Players”):GetPlayers()

Simple.

print(#game.Players:GetPlayers())

1 Like

Ahh I misread the OP, since you want players in ALL servers, the current player count can be done using the provided Roblox API:

https://games.roblox.com/v1/games?universeIds=id

Since you cannot directly send requests to Roblox using GetAsync, you should a use proxy, I recommend rprxy.xyz

https://games.rprxy.com/v1/games?universeIds=id

Use the playing key from the decoded JSON string

--make sure HTTP requests are enabled
local HttpService = game:GetService("HttpService")

local GameDataJSON = HttpService:GetAsync("https://games.rprxy.com/v1/games?universeIds=" .. game.GameId) --we need to use universeID, not Place ID
local Playing = HttpService:JSONDecode(GameDataJSON).playing

PS: Please do not ask for us to write systems for you in #help-and-feedback:scripting-support

12 Likes

Yes, all games. It must be all games. I know that I can do that.

1 Like

(Ok I know that now haha.)

I did this but when I was making a gui that requires 2 or more players in the GAME game, I went on an alt, joined and the gui was there for like 2 minutes and it kept popping back up so I removed the script. I will try it again though just in case.

1 Like

Another way to do this is in my post, using MessagingService. Although, if you’re game gets a lot of traffic, these restrictions will apply:

Messages sent per game server 150 + 60 * (number of players in this game server) per minute

Messages received per topic (10 + 20 * number of servers) per minute

Messages received for entire game (100 + 50 * number of servers) per minute

Don’t edit your posts to close them, this affects search-ability for other users who may have a similar issue, you can revert them by clicking the image icon on your post

Doesn’t work. Thats a bit sad :frowning:

@IntelligentCappy This is not a good option.

You just created your own function to detect the number of players when something already exists for it. You don’t just “for i,v in pairs” here – you’d optimally use ipairs for iteration over numerical indices. Also you don’t need to check for whether the element passed is a Player using a check, you can just use Players:GetPlayers() and that will return only Player objects.

A better idea would be to (as people have already mentioned before me) use the # operator:

#game:GetService("Players"):GetPlayers() --> returns table of players only

and running that on Players.PlayerAdded and Players.PlayerRemoving
to get all players in the current Server.

However the OP wants to get the number of Players in all servers of their game.

The most viable solution seem to be using this API:

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

which returns data about the universe, use a proxy however to access it.

Related :

6 Likes

Ok, thanks for helping 50%

How do I use a proxy for it though?

I agree, what is a proxy???

Necrobump


A proxy server is a server that acts as a medium or intermediary of transmission via requests from a server to a client. Basically, this server, when requests are received, sends a request to the intended or target server endpoint and returns a result written in the proxy.

This is used to send requests to endpoints of the roblox domain from studio and the roblox client, since roblox prohibits requests to their domain if sent from those. A common proxy server for roblox is rprxy.xyz, as FilteredDev has mentioned in the posts above.

To use this proxy server, you would change roblox.com in the domain to rprxy.xyz. Example:

-- Original API URLs:
https://games.roblox.com/
https://groups.roblox.com/
https://api.roblox.com/

-- Original API links with the proxy server:
https://games.rprxy.xyz/
https://groups.rprxy.xyz/
https://api.rprxy.xyz/
4 Likes

Create an IntValue within ServerStorage then call MessagingService:PublishAsync when Players.PlayerAdded and Players.PlayerRemoving, also call MessagingService:SubscribeAsync to change the IntValue.Value :smiley: :smile:

The following API endpoint when given a place ID will provide a list of information regarding that place’s active servers.

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

This can be used to determine the total number of players currently playing a game.

local http = game:GetService("HttpService")
local get = http.GetAsync
local jsonDecode = http.JSONDecode

local proxyUrl = "roproxy.com" --Put your domain's proxy here.
local baseUrl = "https://games."..proxyUrl.."/v1/games/%d/servers/Public?limit=100&cursor=%s"

local function getPlacesPlayingCountRecursive(placeId, playersCount, cursor, maxTries)
	playersCount = playersCount or 0
	cursor = cursor or ""
	maxTries = maxTries or 10
	if maxTries == 0 then return end
	
	local requestUrl = string.format(baseUrl, placeId, cursor)
	local success, result = pcall(get, http, requestUrl)
	if success then
		if result then
			local success2, result2 = pcall(jsonDecode, http, result)
			if success2 then
				if result2 then
					for _, serverItem in ipairs(result2.data) do
						playersCount += serverItem.playing
					end
					
					cursor = result2.nextPageCursor
					if cursor then
						return getPlacesPlayingCountRecursive(placeId, playersCount, cursor, maxTries)
					else
						return playersCount
					end
				end
			end
		end
	else
		warn(result)
	end
	task.wait(1)
	maxTries -= 1
	return getPlacesPlayingCountRecursive(placeId, playersCount, cursor, maxTries)
end

local playersCount = getPlacesPlayingCountRecursive(185655149)
print(playersCount) --19473
5 Likes

This is bad because references to player objects inside of tables don’t get garbage collected if the table is set to nil.

YOu’d have to make sure u remove all the items in the table with

table.clear

so u dont have an unreferenced table floating around the memory with references to things