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.
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
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.
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 icon on your post
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:
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/
Create an IntValue within ServerStorage then call MessagingService:PublishAsync when Players.PlayerAdded and Players.PlayerRemoving, also call MessagingService:SubscribeAsync to change the IntValue.Value
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