How would I send data from one place to another using MessagingService?

I’m trying to send the length of a table to a reserved server, then want to compare if the amount of players in the game is equal to the length of the table.
If it doesn’t make sense, check the scripts below

The issue is that the length of the table doesn’t print in the second script. There’s no error in the output. I’ve been searching for answers on the dev forum for quite a while but haven’t been able to find anything so far. Any help is appreciated, thanks in advance.

Script in the lobby place:

local tpService = game:GetService("TeleportService")
local reservedServerCode = tpService:ReserveServer(9222987139)

local repStorage = game:GetService("ReplicatedStorage")
local queueUiEvent = repStorage:WaitForChild("queueUiEvent")
local players = game:GetService("Players")

local messagingService = game:GetService("MessagingService")
local messagingTopic = "queueSizeTopic"

local joinPart = script.Parent
local queue = {}
local maxLength = 2

	local char = hit:FindFirstAncestorOfClass("Model") -- Character that touched part
	local player = game:GetService("Players"):GetPlayerFromCharacter(char) -- Player that touched part (from character)
	if player and not table.find(queue,player) and #queue < 2 then -- If player's still there and not already in the queue and theres still room
		table.insert(queue, player) -- then add player to the queue
		for i,v in pairs(queue) do -- Notify players inside the queue 
			queueUiEvent:FireClient(v, #queue, maxLength, "fillQueue") -- Fire a remote event to players in the queue, sending queue length and max queue length
		if #queue == maxLength then -- if queue is full, teleport players and empty queue
			local success, errormessage = pcall(function()
				messagingService:PublishAsync(messagingTopic, maxLength)
			tpService:TeleportToPrivateServer(9222987139, reservedServerCode, queue) -- teleport all players		
			for i,v in pairs(queue) do
				queueUiEvent:FireClient(v, #queue, maxLength, "emptyQueue")
			table.clear(queue) -- Empty queue afterwards

And script in the other place:

local messagingService = game:GetService("MessagingService")
local messagingTopic = "queueSizeTopic"

local success, errormessage, connection = pcall(function()
	messagingService:SubscribeAsync(messagingTopic, function(msg)
1 Like

It could be because the message gets sent before the server actually starts up.

You could probably store the length of the table in a MemoryStore so you can just get the value in the other place.

1 Like

Hey, thanks for the reply. I’m not really familiar with MemoryStoreService, could you explain how it works?

Memory Stores are like Data Stores but they’re not persistent (they don’t save forever) and they’re faster than Data Stores.

They provide two data structures, Queues and Sorted Maps.

For this, you would use a Sorted Map, its basically like an OrderedDatastore, you would add a new entry with the key being the reserved server’s id (this is the second argument returned from Reserve Server) and the value being the length of the queue.

You can then get the value through the Memory Store, you can access the server’s id using game.PrivateServerId

More Information about Memory Stores

1 Like

thanks, I’ll try it out and let you know if it works

thank you so much for teaching me about this fantastic service, I can already see myself using it for other cool stuff. Sorry for the late response.