How do you broadcast a message in Lua?

Hello everyone.
Topic: I want to know how would it be possible to broadcast a message, like you can in Scratch’s interface.
Example:

External Media

What am I hoping to achieve?:
I want to learn how to broadcast a message in roblox through one script, and another script can receive that message to then be able to do what it’s scripted to do.
Please help me, and I’m not asking for a whole script. I just want to learn some stuff, and this seemed like a good place to do so. I just need an example.

1 Like

broadcast and wait: BindableFunction | Documentation - Roblox Creator Hub
broadcast: BindableEvent | Documentation - Roblox Creator Hub

You could also use modules, and return the functions which you can then call.

2 Likes

Use a remote event. They’re very useful for broadcasting messages from localscripts to serverscripts vice versa.
You create a remote event in ReplicatedStorage, name it whatever you want and then get 2 scripts. (One is a SERVERSCRIPT and the other is a LOCALSCRIPT.)

--serverscript in serverscriptsrevice or workspace doesnt matter
local remoteevent=game:GetService("ReplicatedStorage"):WaitForChild("YourRemoteEventHere")
if game.Loaded then--senses when the game is loaded for the first time
remoteevent:FireAllClients()--fires to all clients
end

In a local script, you want to recieve that event, so

local remoteevent=game:GetService("ReplicatedStorage"):WaitForChild("YourRemoteEventHere")
remoteevent.OnClientEvent:Connect(function()--recieves the remote event 
--do what you want to do here
end)

There are 3 types of broadcasters which are FireServer(), used in the client which fires a message to the server to recieve, FireClient(player object in here), which is used in the server to fire a message to a particular client, and FireAllClients() which is used on the server to broadcast a message to all clients.

This is a one way communication, if you want to use 2 way, use a RemoteFunction or a BindableFunction.

3 Likes

Thank you for such an amazing reply, one more thing though. Is it okay to have as much remote events in replicated storage as I want? The reason I’m asking this is because I’ve seen some topics where people said that it was the remote events, (presumably), or not, that caused the game to crash on low-end devices. I just want to know if there’s a limit to what I can do.

1 Like

Thank you for sharing the documentations listed here! I’m new to scripting and I need to learn some stuff, mainly because Scratch is boring now, and I know the code blocks aren’t even a percentage of real coding knowledge. I guess functions are, but you know what I mean.

1 Like

It doesn’t matter how many remoteevents you have, as long as you don’t fire too many at once (depends on how much data is being sent, so you’d probably don’t want a remote event being fired in a while loop that has a 0 second wait.) the you should be good

1 Like

Alright, thank you! I have trouble seeing which parts of a script does what, but not a whole lot, since I know some basics. All I really know how to do is use:
wait(1)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.