How to display player count with MessagingService

Hello, I am creating a system in my Roblox game that displays a player count for a specific private server based on the target PrivateServerId. From my knowledge, I don’t think it’s possible to “invoke” another server using MessagingService, so I tried out a system that bounces back data (MessagingService Async) and validates it through the parameters of the Data. This is messy and also doesn’t work too well.

What is another way I can accomplish this using MessagingService? Is there an easier way to “invoke” a specific server and be able to return the player count?

Thanks.

You can’t invoke specific servers due to their limitted hardware meaning basically all transmissions go to every server. So you’d have to go with this

1 Like

Maybe something like that?

Script that will return the amount:

local MS = game:GetService("MessagingService")
local PS = game.PrivateServerId
MS:SubscribeAsync("Order_"..PS, function()
	MS:PublishAsync("Response_"..PS, #game:GetService("Players"):GetPlayers())
end)

Script that asks for the information:

local MS = game:GetService("MessagingService")
local function GetNumberOfPlayers(PrivateServerId: number): number?
	local Players = nil
	local RBX = MS:SubscribeAsync("Response_"..PrivateServerId, function(Amount)
		Players = Amount
	end)
	MS:PublishAsync("Order_"..PrivateServerId, "")
	
	--			Wait			--
	local MaxWait = 10
	while not Players and MaxWait > 0 do  MaxWait -= game:GetService("RunService").Heartbeat:Wait()  end
	RBX:Disconnect()
	return Players
end

you can change 10, keep in mind that it can wait infinitely if the server is not activated.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.