Hello roblox developers!
I saw that many programmers have a hard time learning these concepts, so with this post I will try to make it as clear as possible what RemoteEvents and RemoteFunctions are and how they are used.
What do I need to know to understand this tutorial?
What is the client and the server?
1. Server: The server is the part of the game that all players see, this is modified from the “Script” object, if a part is destroyed from the server (Script), this change will be visible to all users.
2. Client: The client is the part of the game that only one player sees, each player has his own client, this is altered from the “LocalScript” object, for example, if in a LocalScript (Client) you put a part to be destroyed, only the player owner of the client that was just executed, will stop seeing and interacting with this part.
Example video: ClientAndServer
What are these objects?
In the Client (LocalScripts) there are arguments that cannot be used in the server (Scripts) and in the server (Scripts) there are arguments that cannot be used in the client (LocalScripts), so these objects allow us programmers connect these two parts of the game, but how?
RemoteEvents
RemoteEvents allow certain information to be passed from client to server or from server to client and execute a function:
-
From client to server: To execute a function and pass information from the client to the server, first of all, on the client we need to obtain the RemoteEvent (this is usually the ReplicatedStorage service) and use: FireServer () as follows:
The: FireServer is what tells the RemoteEvent to run, in its parentheses you can put information that may be useful like this: , You can put anything (Color, material, string, number, objects …) And finally, on the server, we obtain the RemoteEvent and execute its OnServerEvent event, which detects when the RemoteEvent is executed: If you have sent any information, you must put a name between the parentheses of the (function (), but you must bear in mind that the first parameter of this event is always the player who executed the RemoteEvent, so we put the respective parameters: And now you can put the lines of code that you want to execute, in my case, to show you that information can be sent in this way, I put a sentence with the data previously set: -
From server to client: To execute a client function from the server we obtain the RemoteEvent and now we have to take two things into account, the : FireClient () and the : FireAllClients (), if you want it to only be executed in a client (for a single player), you must use: FireClient (), if you use it, between its parentheses you must indicate which player you mean, otherwise, you use: FireAllClients () you must not define the player, because will execute on all clients, that is, all players:
In both cases you can also put information in the same way that I explained before, and from the client we use the RemoteEvent ** OnClientEvent ** event, this like the OnServerEvent, it detects when it is executed, but this time from a: FireClient () or: FireAllClients (), this event, unlike the other, its first parameter is not required to be the player: And we put what we want to be executed:
RemoteFunction
RemoteFunctions, like RemoteEvents, allow the client to execute a server function and the server to execute a client function, with the difference that after executing this function, it allows more information to be sent to the initial Script / LocalScript.
-
From client to server: To execute a server function from the client, we first need to get the RemoteFunction and use its function: InvokeServer (), which turns on the object:
Like the: FireServer, you can put some information by putting it between its parentheses: Now, to detect when that RemoteFunction is turned on from the client, its event OnServerInvoke is used, but this is no longer accompanied by :Connect (function(), but must be accompanied by = function() as shown in the image: and again, between the parentheses of the function (), like the OnServerEvent, the player must go as the first parameter and then the information sent: and finally write what we want to be executed: But that’s not all, as I said before, these objects are characterized by returning information to the initial Script / LocalScript, this is done using the return with the information you want to pass back: And now the Remote: InvokeServer () that we put in the client, will be equivalent to what we put in the return, so, to check it, I will be able to print it and it will have to put the number 32 in the output: -
From server to client: And finally, a RemoteFunction also allows you from the server to execute a client function and return more information, for this, from the server we obtain the RemoteFunction and we turn it on using :InvokeClient () in this way:
Like the: FireClient (), we need to specify which player we mean, so we put it between its parameters: And we can also specify some information: Now, to detect when the RemoteFunctuion was fired on the server, we will use its OnClientInvoke event, which like OnServerInvoke, this one is executed with = function (), but its first parameter is no longer the player, but the information sent from server: And now we put what we want to be executed, in my case a print: And now again we put at the bottom a return with the information that we want to return to the server and that will make the: InvokeClient equal to that information so to teach it I will put it in a print: