How would I go about developing game-wide broadcast system for developers via MessagingService?

Hey, everyone. I would say I’m definitely not fluent in Lua scripting/programming which is why I need help with this.

So, what I want to basically achieve is a textbox which I can write into and when I press a certain “broadcast button” it will send a message in a text label to all servers using MessagingService. I don’t want the actual code because that’s obviously not this category’s purpose, but just some guidance on how I would start on this and successfully develop it. I can’t find more than the PublishAsync and SubscribeAsync, which also can’t fully understand in the MessagingService tutorial, I have also searched for a tutorial specifically about broadcast, but as expected I got no results there, so that brings me here to this category.

I would appreciate if someone could help me, and this is my second topic on the developer forum as I was just accepted a few days ago so, if I make stupid mistakes, do tell me so I can fix them and settle better into this wonderful community.

1 Like

If you don’t know how to use messaging service, then you could follow this tutorial made by @Alvin_Blox

2 Likes

I’ll definitely have a look.

Thanks for a speedy reply.

1 Like

Since you need the input from localscript you will also need remote events. Make sure you have some kind of cooldown and that you filter the text!

1 Like

How would I encode or implement the text box and text label part into the info given from that tutorial?

Considering you need a remote event anyways you would send the text to the server. Assuming you have to press enter for the message to send, I would do it like this.

box.FocusLost:Connect(function(enter_pressed)
    if enter_pressed then
        remote_event:FireServer(box.Text)
    end
end)

and on the server

-- I would recommend a cooldown since messaging service has limitations!
remote_event.OnServerEvent:Connect(function(player, message)
    -- filter the message, then send it!
    MessagingService:PublishAsync(topic, filtered_message)
end)

So just so I can understand this better;

“box” is a variable which has script.Parent (the textbox for devs) in it, and what is FocusLost? is it a function or what? Additionally is enter_pressed also a variable which has UserInputService to connect a function when the “Enter” Input begins?

Apologies if I sound stupid, I am a beginner scripter still.

Sure, assuming it is a localscript under the text box.

FocusLost is an event that fires when the client loses focus of the text box. enter_pressed is a boolean passed to your listener function. When you press enter while focused on a text box (assuming MultiLine is disabled), you lose focus.

1 Like

Is “remote_event” something I name?

And where do I place the remote event, replicatedfirst?

You define remote_event as a remote event, yes. You would place it in ReplicatedStorage, since it is a container whose contents are shared between client and server.

1 Like

Mk, hopefully this is the last thing I am forced to annoy you with, but, to be clear, i’m going to do the messagingservice SubscribeAsync and PublishASync in a script in ServerScriptService.

And do I define “filtered_message” as the message I want to send and topic as the topic?

Yes, that would all be correct.

HOPEFULLY last one.

Do I wrap the SubscribeAsync in a pcall?

You definitely should since you have no control of whether or not the subscription is successful.

1 Like

Server (serverscriptservice):

-- variables 

local MessagingService = game:GetService("MessagingService")
local TOPIC = "GlobalBroadcast"
local GlobalBroadcast = game.ReplicatedStorage.GlobalBroadcast
local Box = game.StarterGui.ScreenGui.Frame.BroadcastMessageInput.Text

-- other stuff

local subscribeSuccess, subscribeConnection = pcall(function()
MessagingService:SubscribeAsync(TOPIC, function(message)
end)

wait(5)
GlobalBroadcast.OnServerEvent:Connect(function(player, message)
	FilterStringAsync(Box)
	MessagingService:PublishAsync(GlobalBroadcast, Box)

Client: (under the dev textbox input thing)

-- variables

local Box = script.Parent

local GlobalBroadcast = game.ReplicatedStorage.GlobalBroadcast

Box.FocusLost:Connect(function(enter_pressed)

if enter_pressed then

GlobalBroadcast:FireServer(Box.Text)

end

end)

Theres a 99% chance there could be something wrong, have a look please :slight_smile:

I’m going to assume that you have FilterStringAsync defined as a function elsewhere, or else this will not work.

Also, make sure you check the UserId of the player who fired the remote against a table of developer user IDs (or check their rank in a group depending on if you have a group) to prevent exploiters from firing the event and spamming announcements.

Also, when you are publishing, you need to use a string and not an Instance. Try this:

MessagingService:PublishAsync(TOPIC, Box)

Lastly, you are missing an end in your pcall. Try this:

local subscribeSuccess, subscribeConnection = pcall(function()
    MessagingService:SubscribeAsync(TOPIC, function(message)
    end)
end)

Although, to be honest, I don’t think you need a pcall

1 Like

For the time being i’ll leave checking who fired the event considering I’m only doing this for test purposes.

Also, I did the FilterStringAsync wrong, what do you mean by defining it as a function?

Calling FilterStringAsync by itself will not be effective, as filtering is a part of TextService.

I’ve written a new script for you that should work well.

local MessagingService = game:GetService("MessagingService")
local TextService = game:GetService("TextService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local BroadcastEvent = ReplicatedStorage:WaitForChild("GlobalBroadcast")

local BroadcastTopic = "GlobalBroadcast"

local function FilterStringAsync(Text, Player)
	local FilteredText = "" -- Nothing yet
	local Success, Error = pcall(function() -- Wrap in pcall in case filtering API errors
		local FilteredTextResult = TextService:FilterStringAsync(Text, Player.UserId)
		FilteredText = FilteredTextResult:GetNonChatStringForBroadcastAsync()
	end)
	return FilteredText
end

MessagingService:SubscribeAsync(BroadcastTopic, function(message)
	-- Do stuff with "message" here
end)

BroadcastEvent.OnServerEvent:Connect(function(player, message)
	local StringToSend = FilterStringAsync(message, player)
	MessagingService:PublishAsync(BroadcastTopic, StringToSend)
end)

To note a few things I’ve changed:

  • Previously, you were using the StarterGui to get the text, but this cannot be done because StarterGui does not represent the given player’s GUI. This is why we used a Remote, to send the text from the client to the server.
  • Removed pcall from SubscribeAsync because I do not believe that it will cause any major issues, and just clutters the script.

Technically since it’s still developer set text you wouldn’t need to filter it, after making sure that you’re the only one who can fire the remote and not just any random.

No, you should always filter any string that is sent by any player for display to any other player. If you do not filter input that comes from any client, regardless of their status as a developer or staff member, your game could be taken under review until you correct the issue.