Getting number of players in current Reserved Server

Before a player joins a reserved server, I need to make sure there are open slots. If there aren’t, then it will create a new reserved server, if there are, then the player will be placed into that slot. However, when the player makes a reserved server I save the access code in the GlobalDataStore in the main game so that other players can join it from other servers. But when the player leaves the reserved server or another player joins, I need it to ping the main game, informing it how many players there are.

Player leaves or a player joins the game, events fire:

  • Pings the server how many players are in the game via MessageService.

I’m having two issues with that.

  1. In the main game, it saves the access code, but when the player joins and I print out the access code (In the new reserved server), it is a completely different access code. (So when I ping the main server via MessageService it’s getting a different Access Code then the one it saved in the main game.)

  2. In the MessageService, you can send data. But how do I send two different messages in the ping: For example, I need to send over the Access Code and the Player list. This way it knows which Reserved Server it’s getting pinged from.

2 Likes

Looking at the devhub page for PublicAsync, the message parameter takes in a tuple. Meaning you can easily send that information by sending the access code, and then a table containing the names of all the players in that reserved server.

Sample code
-- Assuming accessCode is defined

local Players = game:GetService("Players")
local MessagingService = game:GetService("MessagingService")

function getPlayerNames()
	local t = {}

	for _,player in next, Players:GetPlayers() do
		t[#t +1] = player.Name
	end

	return t
end

MessagingService:PublishAsync("ServerUpdated", accessCode, getPlayerNames())
1 Like