Ran into an issue with the MessagingService

I want to write a script to send a message from one server to the other servers so that the other servers can be notified and update some counters showing in my game scene.

My messages sending script:

local MessagingService = game:GetService("MessagingService")

local function sendMessage(topic, data)
	local success, errorMessage = pcall(function()
		MessagingService:PublishAsync(topic, data)
	end)

	if success then
		print("Message sent to topic:", topic)
	else
		warn("Error sending message:", errorMessage)
	end
end

task.wait(5)
-- Example usage
sendMessage("GlobalAnnouncement", {message = "A new event is starting soon!"})

My message receiving script:

local MessagingService = game:GetService("MessagingService")

local function onMessageReceived(message)
    print("Received message:", message.Data.message)
end

local function subscribeToTopic(topic)
    local success, connection = pcall(function()
        return MessagingService:SubscribeAsync(topic, onMessageReceived)
    end)

    if success then
        print("Subscribed to topic:", topic)
        return connection
    else
        warn("Error subscribing to topic:", connection)
    end
end

-- Example usage
local connection = subscribeToTopic("GlobalAnnouncement")

-- Clean up when the script is terminated or the player leaves
game.Players.PlayerRemoving:Connect(function(player)
    if connection then
        connection:Disconnect()
        print("Unsubscribed from topic.")
    end
end)

My console output:

  10:10:48.826  Subscribed to topic: GlobalAnnouncement  -  Server - MessagesReceiver:13
  10:10:52.940  Message sent to topic: GlobalAnnouncement  -  Server - MessagesSender:9

My message is sent successfully, since the pcall returns true as the first argument. However, the onMessageReceived(message) method is not invoked at all.

Did anybody experience this before or anybody has a clue to the solution?

1 Like

I’m pretty sure it is

print("Received message:", message.Data.Name)

Umm… this is the table I sent via the service.

{message = "A new event is starting soon!"}

The issue is not regarding to what I sent. onMessageReceived(message) is not invoked.

There may be a problem with the message parameter for your onMessageReceived() function.

Have you tried nesting the function within the :SubscribeAsync() method itself? This may not fix your issue, but it’s worth a try:

local success, connection = pcall(function()
    return MessagingService:SubscribeAsync(topic, function(message: {any}) -- Instead of defining a function externally, try nesting it within :SubscribeAsync().
        print("Received message:", message.Data.message)
    end)
end)

Nope, the issue is still there. XD

Still have no clue to fix the issue

MessagingService does not function in studio, only in-game. This code all works correctly if you test it in the actual game. Cheers

1 Like

I’m sure that the code works on Team test a few months ago and it does not run on Team Test rn. That’s why I submit this issue XDDD