Introduction
Recently Roblox released a new service called MessagingService, this allows for you to easily communicate between all of your running servers. How is this useful? Users can do so many things now that before were extremely complex and typically required an external webhost/database, things like custom matchmaking and crossover chats.
In this tutorial I will be teaching you how to use MessagingService to create a crossover chat. I will be releasing a tutorial in the near future about how you can make your own cross server matchmaking using MessagingService
Disclaimer This is a new service so there is always the off chance that something may not function properly or will be changed in the future!
Functions
- PublishAsync(key, data) sends out data to be received by all other servers.
- SubscribeAsync(key, callback) receives data on all servers
Example
Cross Server Chat
So, how do we create our cross server chat? Let me start off by saying that this is not a UI Tutorial or a Custom Chat system tutorial, this is strictly a basic example on how to communicate between servers. If you are looking for a UI tutorial then I would suggest reading @EmeraldSlash’s
The first thing we need to do is detect when the player chats, to do that we can use Chatted
local Players = game:GetService("Players") -- Player Service in order to detect when a user joins
Players.PlayerAdded:connect(function(player) -- Detecting when a player joins
player.Chatted:connect(function(msg) -- Detecting when a message is send
-- When a user chats whatever is inside of here will be called
end)
end)
There we go! A basic chat detection but you most likely already knew about that, so lets get in to the juicy stuff! Sending out data for every server to read is very simple all you need to do is PublishAsync your data, however tables nor objects can be sent between two servers, this means we’ll need to encode our table before sending it off. We can use JSONEncode
So first we need to create our table that will contain what our message says as well as the user who sent it.
local messageData = {
sender = player,
message = msg,
}
There we go! Now we have our table. Be sure to filter the message sent though!
Now we need to encode our table and send it out to all of the servers, simple enough right?
local httpService = game;GetService("HTTPService")
local encoded = httpService:JSONEncode(messageData)
Just like that our data has been encoded and it’s ready to be sent out! So lets do that now by using PublishAsync. Now don’t forget that it requires a key before our data that we’re sending, we’ll use “Chat” for our key.
local messagingService = game:GetService("MessagingService")
messagingService:PublishAsync("Chat", encoded)
And just like that half of our code is already done, great! We’ll now need to catch all of our sent data, we’ll use SubscribeAsync for that. It requires our key as well as a callback function.
function callbackFunction(serviceData)
end)
messagingService:SubscribeAsync("Chat", callbackFunction)
serviceData in our callback function is actually a table containing two values data and sent, Data being what we sent and Sent being the tick in which the request was made.
Now lets finish the system up and send out our chat message!
local decodedData = httpService:JSONDecode(serviceData.data)
print(decodedData.sender.." : "..decodedData.message)
With all of this information you could do so much more then what I’m showing, but this is just a basic rundown! I hope you learned something and now have a general understanding of how to use this service, if you don’t then please ask questions!
Finished Product
local Players = game:GetService("Players")
local httpService = game:GetService("HttpService")
local messagingService = game:GetService("MessagingService")
function callbackFunction(serviceData)
local decodedData = httpService:JSONDecode(serviceData.data)
print(decodedData.sender.." : "..decodedData.message)
end)
Players.PlayerAdded:connect(function(player)
player.Chatted:connect(function(msg)
local messageData = {
sender = player,
message = msg, -- filter message first!
}
local encoded = httpService:JSONEncode(messageData)
messagingService:PublishAsync("Chat", encoded)
end)
end)
messagingService:SubscribeAsync("Chat", callbackFunction)