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.
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.
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.
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.
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
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)