RemoteEvents and RemoteFunctions

Hello roblox developers! :grinning:
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

Roblox Client-Server Model

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: Captura de pantalla 2021-06-28 170302 , 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: Captura de pantalla 2021-06-28 171238 Captura de pantalla 2021-06-28 171310

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

11 Likes