Messaging service

The system will send the message to every server on your game!

by DoiGaMay

Author Avatar

Hello people who are learning Lua, my name is DoiGaMay, today we are going to learn about Messaging service


What is Messaging service?


Let's give an example! So your game has 5 servers, and what you wanted to do let every server on your game to print This is a message for all servers You can send the data via messaging service. Once the server receive the data the server will print it!

But how am i gonna do that? It's pretty simple, first you have to learn how to use it first!

Understanding few functions

First, let's get a variable then we get the MessagingService with this code!

local MsgService = game:GetService("MessagingService")

After that we going to make a function when the server receive SubscribeAsync!

MsgService:SubscribeAsync("Message",function(Data)
end)

This following code will only read the data when one of the server send data on "Message"! You can change to anything you want!

And then we going to make a function that will send the data with PublishAsync!

MsgService:PublishAsync("Message","Your data table here")

The following code will send the data to "Message" with Data table!

Congratulations, Now you probably know how PublishAsync and SubscribeAsync! We gonna move on to the actual scripting.

Coding the Messaging Service

Now here's an example to let the server print anything you want to.

local MsgService = game:GetService("MessagingService") 

--- RECEIVE DATA

MsgService:SubscribeAsync("Message",function(Data)
      if Data.Data.WhatToPrint ~= nil then -- Check if WhatToPrint is not nil
        print(Data.Data.WhatToPrint) --- PRINT DATA
        --[[ 
           To get the data you must access to it by add .Data to it
           Example this function is function(Data)
           you have to put Data.Data to access the message data!
        ]] 
      end
end)

--- Now we going to send the data!

MsgService:PublishAsync("Message",
  {
 WhatToPrint = "Hello World" --- Send WhatToPrint to "Message"
  }) --- You can add more data into table example : {WhatToPrint = "yes",WhatToPrint2 = "Yes"}

After that, now you have the code which will send the data to every server and let the server print Hello world with the code print("Hello world")!

THANK Mark_GoodMan for rewritting this tutorial to make it easiler to understand!

View in-game to comment, award, and more!