I want to make it so if a script uses messagingService:PublishAsync("Topic", "Message) to publish something, how would I make it so, when a script is subscribed to the topic it will return something back to the script that published the async?
You can always listen on the server who initially sent the request on a second Topic which the other servers can send the data back to.
messagingService:PublishAsync("Topic", {
JobId = game.JobId,
Message = "Message"
})
-- Receive responses
messagingService:SubscribeAsync("TopicResponse", function(data)
if data.JobId == game.JobId then
-- This response is for our server
end
end)
mmk @incapaxx, and @ReturnedTrue. So it would look something like so:?:
local msgService = game:GetService("MessagingService")
messagingService:PublishAsync("Topic", "I am messaging you about this. ... ")
local disconnect = false
local success, errorDubug, connection = pcall(function()
messagingService:SubscribeAsync("ReturnTopic", function(message)
disconnect = true
end)
end)
if disconnect then
connection:Disconnect()
end
messagingService:SubscribeAsync("Topic", function(message)
if message.Data == "Something" then
messagingService:PublishAsync("ReturnTopic", "I am returning this. ...")
end
end)
You’re getting the right end of the sticck here, basically you send for it to send back.
Just make sure you include the JobId checks like @SimplyData showed.
FYI: You’d need to return the SubscribeAsync to get the connection which would be the second argument returned anywho.
You might as well disconnect the connection inside itself:
local Connection do
Connection = MessagingService:SubscribeAsync("Topic", function()
Connection:Disconnect()
end)
end