Help on making a global message system

  1. What do you want to achieve? Keep it simple and clear!
    I wan’t to make a script where you can type something and will show in every server and will not dissapear until i change it.

  2. What is the issue? Include screenshots / videos if possible!
    I can make the script of typing the message but i dont know how to show in every server. I noticed something called MessageService, the reason i dont use is below.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I always try to see into the dev hub, posts. But i dont learn too much by reading, if you could explain me in other way. Thanks.

Don’t make the entire script, i just need how to send the message to all the servers

Thanks.

11 Likes

I think you might need to utilize the MessagingService

Yeah i know, i think i should use it but i don’t know how to use it. As i said, i can’t understand the developer hub. If you know how do i use it thanks.

It seems like this is what you need!

1 Like

A global message system will require MessageService (will not relink).

The API is fairly simple, actually. You have two events that you’re able to use: PublishAsync and SubscribeAsync. PublishAsync is intended to send a message while SubscribeAsync is intended to receive a message. Messages are transmitted by topic name and when retrieved, will give you a set of data that you will be able to take out.

When you use either method, the main point you’ll want to hone in on is the topic. From there, the second argument changes based on what function you’re going to be using.

PublishAsync will allow you to send a message over. A message is an arbitrary datatype worth of content that you wish to transmit over the server, though it must be a pure primitive. This is then JSONEncoded and sent across your game to all places and all servers. You do not need to do any encoding yourself here.

SubscribeAsync will allow you to receive and handle a message. It accepts a callback function which will get passed with a single parameter, the message sent over. You can access this through the data member of the table you access from that parameter. I do believe this is automatically unencoded when it’s received at the target place.

Roblox has a guide on cross-server messaging that could help you towards understanding MessagingService: Cross-Server Messaging Guide.

5 Likes

I saw the code but is broken. Error prints me: argument 1 missing or nil

1 Like

I just started on scripting, i literally don’t know what’s cross server messaging guide, i saw the post you linked but it doesnt explain how do i edit it to create a global message system.

Also, when i said global message system i meant a global announcement or where you can say something (only you) and everyone will recieve the message

1 Like

Of course it’s not going to hand your exact use case to you on a silver platter: it’s meant to be an example that you can salvage. It explains very clearly how to create a global message system and I even offered up an explanation myself.

The method of retrieving a message that a player wants to send is up to you. As for delivering that message to complete your global announcement system, that’s where this code steps in.

Ideally for the PublishAsync (sending) bit, you’ll want your topic to be GlobalAnnouncement and the message to be whatever the player wants to announce. As for the SubscribeAsync (receiving) bit, this is pretty much the same thing. Subscribe to the topic GlobalAnnouncement with a function accepting one parameter, then extract the message out of its Data field.

MessagingService:PublishAsync("GlobalAnnouncement", "foo bar")

MessagingService:SubscribeAsync("GlobalAnnouncement", function(message)
    print(message.Data)
end)

Make sure that you’re using pcall with these events as well. MessagingService uses best effort <1 second to deliver messages but the time a server receives a message is not guaranteed, nor is the fact that a server will receive a message. Your game should be built so that a MessagingService failure does not critically impact game systems.

1 Like

Ok i have my questions:

  1. message i can change it to the message i want right?

  2. How do i fire the script? i mean, because i want to have a button or something of that type and when you use it, the announcement will show, how i do it? as i see, i can use something like this:

MessagingService:PublishAsync("GlobalAnnouncement", "foo bar")

MessagingService:SubscribeAsync("GlobalAnnouncement", function(message)
if --[[Player has clicked or touching a block]] then
    print(message.Data)
end
end)
1 Like

The message can be changed to whatever you want. As for running the script, you just need to incorporate those functions somewhere in your game. Use PublishAsync to send messages and SubscribeAsync to receive them.

Making the button is something else you need to do. I could provide example code for it though: adding anything else (from security checks to other functionality) is all up to you though.

-- LocalScript for a Gui:
TextButton.MouseButton1Click:Connect(function ()
    RemoteEvent:FireServer(TextBox.Text)
end)

-- Script in ServerScriptService:
RemoteEvent.OnServerEvent:Connect(function (player, announcement)
    local message = {user = player.Name, msg = announcement}
    MessagingService:PublishAsync("GlobalAnnouncement", message)
end)

-- Script somewhere else, like another game:
MessagingService:SubscribeAsync("GlobalAnnouncement", function(message)
    print(message.Data.user.." says "..message.Data.msg)
end)
12 Likes

For now it’s working, i write something and it prints, right now im testing it on Studio in a solo server (cause if i make other server, the pc may crash [my computer is not fast]) and by any reason, it prints 2 times, you know why?

[NEVERMIND I FIGURED OUT WHY]

but my question is that the announcement will save? if i create other server?

3 Likes

MessangingService isn’t a save and load later, it’s just a function that runs when told to really. If you want it to save, save the message content to a datastore and call it on the new created server.

1 Like

How i do that? i know datastore saves stuff but it can save stuff for the game? Can you tell me something about it? thanks

1 Like

This article tells you everything you need to know to work with datastore service.

2 Likes

Well, im not to good at reading articles. by the way, i will ask the same question in other post. Thanks for helping!

1 Like

It doesn’t make sense to ask a question that’s been answered in another post because you don’t want to take the time to read…

1 Like

Well, i read it, but i don’t understand it. I’m not good learning stuff by reading.

EDIT: I’m trying to create a datastore with the stuff i read

2 Likes

This is the script i made but it error prints that key is empty

local DataStoreService = game:GetService("DataStoreService")
local msgstore = DataStoreService:GetDataStore("MessageStore") --Value that saves the message
   
local success, err = pcall(function()
	msgstore:GetAsync(game.Workspace.GlobalAnnouncement.Value)
end)

local key = game.Workspace.GlobalAnnouncement.Value   

if success then
	print("Success!")
else
	msgstore:SetAsync(key, "ohnub")
end

If you want you can help me, thanks.

2 Likes