how do i get the value of the TOTAL playercount of my game using HttpService?
I’ve tried using the api stuff but did not figure it out. any advice?
how do i get the value of the TOTAL playercount of my game using HttpService?
I’ve tried using the api stuff but did not figure it out. any advice?
i think you can just use the playerservice.PlayerAdded event and get the number of players
local numplayers = 0
game.Players.PlayerAdded:Connect(function()
numplayers+=1
end)
game.Players.PlayerRemoving:Connect(function()
if numplayers~=0 then numplayers-=1 end
end)
i guess…
Edit: my bad sorry that only counts the player count in one server
This wouldn’t be the total player count, this would only be the players in the current server. She wants the total player count in all the servers.
local players = game.Players:GetPlayers() -- Get all the players in the server.
local amt = #players -- get the amount of the players ( in number )
print(amt) -- Prints the amount
lets say there are 5 player in the server so the output will be:
5
because we print the amount of the players in the current server.
To make it in one line you can try this:
print(#game.Players:GetPlayers())
I think she wants the number of players in EVERY server.
no, i don’t think so. they maybe want for the current server…
for ingame leaderboard stuff, Live player counter(probably)
Thanks for clarifying.
You would need to communicate along the servers.
I made something like that when I learned about MessagingService. I think you could also do this with HTTPService.
local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local totalPlayerCount = 0
local function onReceive(msg)
totalPlayerCount += msg.Data
end
MessagingService:SubscribeAsync("PlayerCount", onReceive)
Players.PlayerAdded:Connect(function()
--Subscribe to the topic to portray the amount of players in the game
--Tell all servers that a player joined
MessagingService:PublishAsync("PlayerCount", 1)
end)
Players.PlayerRemoving:Connect(function()
--Tell all servers to remove one player
MessagingService:PublishAsync("PlayerCount", -1)
end)
while true do
wait(1)
print("The total player count is " .. totalPlayerCount)
end
Replace the while true do loop with something better, I just did this for testing out MessagingService.
thank you. this solved my problem
Recommendation: if you were to try and get the number of players don’t do that. It’s harder and less efficient, instead do #game.Players:GetPlayers()
Putting a # on the BEGGINING of it gives you the amount of data inside a table.
You could also use this messaging service and send a request to all servers to return the amount of players inside each one, then calculate all those values; since a system like this could break after some time.
Yes. This is much more reliable and efficient. In case for example a server shuts down unexpectedly, using the previously shown method would invalidate the count.
Not only that, but using that previously shown method would mean each new server starts a count from 0, and would give an incorrect result.
(This also means there is a possibility for the count to go below 0.)