Server Messaging Service - for all your script-to-script data sharing needs!

Hello everybody! I’d like to introduce you to Server Messaging Service, or SMS for short (yes, i know).

SMS is kind of like MessagingService, but for the local server. It’s great for sharing data and communicating between scripts, is fairly simple to use.

Let’s talk about the functions, shall we?

                           --//   Functions   \\--

: PublishAsync(topic, data) 
--//
returns: nil

Sends/publishes the data to all that are subscribed to the specified topic.

: SubscribeAsync(topic,callback) 
--//
returns: a special ID, which can be used to call :UnsubscribeAsync()

Subscribes to a topic, and will call the function callback() when data is published on that topic, with the first argument as the data published.

: UnsubscribeAsync(topic,_id)

Unsubscribe from the specifed topic, must include id from :SubscribeAsync()

                           --//   Usage   \\--

eg.

Let’s say we have a script, which will we call Script1, and it has a table called “CFrames”

We have a another script, which we will call Script2, that wants to grab the table “CFrames” from Script1

Oh no, what to do?

Oh! I know! (says script2) I’ll just use SMS to get my data!

Script2 begins typing code into himself -

local SMS = require(game:GetService("ServerScriptService"):WaitForChild("Server Messaging Service")) -- WaitForChild reduces my risk of erroring! 
local CFrames

local function callback(Cframes)
      CFrames = Cframes
end

local id = SMS:SubscribeAsync("Pass the CFrames!",callback)

(Script2) “Hey Script1! Can you pass the CFrames?”

Script2 then tells Script1 to write this code into himself-

local SMS = require(game:GetService("ServerScriptService"):WaitForChild("Server Messaging Service"))
local CFrames = {--[[some juicy CFrames :D]]}

SMS:PublishAsync("Pass the CFrames!",CFrames)

Script2 gets his CFrames

(Script2) “Thanks!”

                             --//The End\\--

Feedback is greatly appreciated!

Enjoy!

https://www.roblox.com/library/5123719738/ServerMessagingService

11 Likes

What makes this better than just standard bindable events?

9 Likes

This does the same thing, but without having to make multiple bindable events.

3 Likes

Can you use this to communicate with other players in a server? The post does not really make it that clear, and I am curious about this if it does.

1 Like

Yes, you should be able to if you use this module on the client(s), and each player has a unique topic, which can then be used by other players to communicate.

2 Likes