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?