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:

4 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.

4 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()
3 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())
2 Likes

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())

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

8 Likes

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

(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.

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 do the classic "for i,v in pairs " , here 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 already mentioned before me, use the # operator as in

#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 seems to be as @FilteredDev mentioned before ,
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 :

1 Like

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/
2 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: