I need a detailed explanation on how MessagingService works

Hi DevForum,

I came across MessagingService a couple days ago, and am still a little but confused on it, and exactly how it works. Would anyone be kind enough to give a detailed explanation?

For example also, spawning a part for all servers with messagingservice as an extra if anyone can.

Thanks!

1 Like

Why do you need a detailed explanation?? I just use the docs for every question I have it doesn’t leave anything out from my understanding. Just try coding it or copy someone else’s code ( if you want to learn it type it out ).

docs: MessagingService | Roblox Creator Documentation

That’s what I’ve been doing and why I need an explanation, because its hard to understand, especially if you want to pull complex functions through.

Hmm okay
well the basic idea is that you are able to publish a message with a tag “Topic”
and every SubscribeAsync takes the message if the topic is the same

Is there something specific you don’t get about it? I don’t really know how to explain this in detail.

Well I understand that for the most part, thanks, but it’s just the function part I don’t get, first there’s a callback on :SubscribeAsync(), and then there’s also a message for :PublishAsync()

For example, how would I go about making a part like this, would this be good?

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

function makePart()
	local part = Instance.new("Part",workspace)
	part.Position = Vector3.new(0,0,0)
end

messagingService:SubscribeAsync("DonationTopic", makePart) -- callback here

messagingService:PublishAsync("DonationTopic") -- message here, purpose i dont understand if we are using a function

Or would there be a better way?

1 Like

Well that looks like it would work. I don’t think it requires a message for the function to be triggered.

and this should work for all servers with this messaging service connected to a subscribeAsync.

You could try passing the donation amount through the message or something

it would just become a parameter on makePart(message)
and it would be like message.Data or something

2 Likes

But yeah this is the idea for messaging service just a subscribe and publish to talk to every server

MessagingService messages travel across all servers, each server can hook a function with SubscribeAsync and a server can call PublishAsync to call the function each server hooked

1 Like

I see, thank you both. So PublishAsync doesn’t necessarily always require a message, unless dealing with certain parameters?

1 Like

Well I havent tested that, but I assume it would send an empty message to all servers.

So it probably works, but if it doesnt just send “hi” or something lol I guess that could be your “empty publish” lol

If I subscribe to a topic, then I post a message, will the sending server also get this message?
I have a game that uses two different topics for messaging, one it seems like the sender does not get their broadcast message, and the other it does…

Example:

Script 1
This script will print yes twice

subscribe("topic_a"(
print("yes")
)
somethinghappens:Connect(
print("yes")
Publish("topic_a")
)

Script 2
This script will print yes once, even though it is the same thing

subscribe("topic_b"(
print("yes")
)
somethinghappens:Connect(
print("yes")
Publish("topic_b")
)