Can someone explain to me what, when and how to use the OnClientEvent like I’m 3? I don’t quite understand the explanation on the developer hub.
Its just the OnServerEvent, but for passing data on to the client instead of the server.
Instead of doing FireServer in a local script, you do FireClient in a server script.
This is very useful for if you want to change playerGuis.
There are two computers running the game you play. One is the server that Roblox runs and the other is you the client (and one for each other player in the game). Events are triggered when something happens so you can set listeners to activate some code when they are triggered. The event OnClientEvent is fired on a remote event when a server script calls the functions :FireClient(a specific client) or :FireAllClients. Its all done by the scripts that you make. So you can make a server script that will run on the server and send information to clients through that event.
Essentially, OnClientEvent:Connect()
is activated when the Server fires a RemoteEvent to a player.
If you’re familiar with Instance.Changed:Connect()
, it has very similar usage to Event.OnClientEvent:Connect()
.
-- Server:
remoteEvent:FireClient(player) -- The server fires a "message" to player
-- Local:
remoteEvent.OnClientEvent:Connect() -- The player receives the "message".
You can use this in a localscript if a serverscript fires FireAllClients, Similar to if a localscript fired FireServer.
Its just little event what can trigger remote event and on Client side if it connected, something will happen.
You said to talk like you’re three, so here it goes.
General Info
Basically, RemoteEvents are a way for the Client (the players in the game, along with their screens) to communicate with the Server (Which is what generally manages block placement, leaderboards, etc.). You usually place RemoteEvents in ReplicatedStorage.
FireServer() and OnServerEvent
Not that you asked, but I’ll still provide info in case you’re new to RemoteEvents in general.
If you wanted to make a “Purchase” button in a shop that subtracts from your currency (Let’s just use Gold as an example), you would be using Client → Server communication. Why? Because, when a player clicks on a button, you use a LocalScript to detect when the player clicked. A LocalScript functions on behalf of the player, while a normal Script (also called a ServerScript) functions on behalf on the server. So, you would use a LocalScript to detect when the player clicked the button. Then, you would use RemoteEvent:FireServer(variable)
to let the server know that the player clicked it. The parameter “variable” I used is just a placeholder that you can also send variables. On a separate ServerScript, you would use RemoteEvent.OnServerEvent:Connect(function(variable)
. This essentially means that it will run a function once the RemoteEvent’s signal has been detected by the Script. If the RemoteEvent also sent information (in this case, a variable) that would allow the server to handle it as needed. Then you would use the necessary code to give the player the item they purchased while also subtracting from their balance.
*Sidenote*: Try to give the client the least power in this communication. If a variable is sent from Client to Server, a hacker can abuse this by changing variables in the event to give them their desired results.
FireClient() and OnClientEvent
Essentially this is the reverse of FireServer
and OnServerEvent
. This allows the server to communicate with the Client. Note that the sidenote provided in the last section does not apply here because the Client has no control over this communication. You might want to use this if you wanted to, let’s say, show a GUI once the player touches a certain block.
You could use something like this (Assuming the Script is a child of the desired block):
script.Parent.Touched:Connect(function(player) -- When the part is touched, the function starts
game.ReplicatedStorage.RemoteEvent:FireClient(player) --Sends a signal to the client (the player)
end)
Note that you can also use FireAllClients()
which essentially sends the same signal, but as provided in the name, fires to all clients.
Then in a LocalScript, you could do something like this:
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function()
--Execute whatever code you want here, it will run when a client signal is detected
end)
Sorry for writing an essay but I just tried to be as descriptive as possible.
Oh no worries at all, this was exactly what I needed. A detailed explanation of the ins and outs of everything.