Get number of players in server by JobId

I have recently learnt that we can teleport players and get the server they are in using a JobId. I’m implementing a matching system where new players can enter existing players to play as long as the server is not full. Is there a way I can check the number of players in a server-based on its JobId?

Sadly, no built-in feature for this, but you can use DataStores to save each player count under each server’s game.JobId

Sorry for replying to an old post, but for any future devs that need this, using Datastores is not a good idea. Instead, use MessagingService to respond to any questions given to a server

Example (not tested):

-- Server that we want to know the number of players of
local JobID = game.JobId
local MessagingService = game:GetService("MessagingService")

MessagingService:SubscribeAsync(JobID.."_NumPlayers", function(responseTopic)
	MessagingService:PublishAsync(responseTopic, #game.Players:GetChildren())
end)
-- Server that wants to know
local MessagingService = game:GetService("MessagingService")

local NumPlayers
MessagingService:SubscribeAsync(JOBID.."_NumPlayersResponse", function(response)
    NumPlayers = response 
end)

-- Ask server
MessagingService:PublishAsync(JOBID.."_NumPlayers")
1 Like