Let's learn how to use remote events!(REWRITTEN AND IMPROVED TUTORIAL!)

Heads up: IF YOU HAVEN’T LEARNED ABOUT LOCAL SCRIPTS AND SERVER SCRIPTS I RECCOMEND YOU LEARN THEM FIRST !

BTW I changed the whole paragraph as some people found missing parts ^^

Welcome!

Today, we are going to learn how to use remote events in Roblox Lua scripting. Let’s understand what remote events are, how they work, and their strengths!

  1. Remote events are events that facilitate communication from the client to the server. (For an in-depth explanation of clients and servers, you can watch this video: Client and Server Explanation)
  2. Players can command the server to perform certain actions using remote events.

Remote events are vital to your creations as they enhance your game, providing flexibility and potential for creating various systems.

Visualize what happens and see an example.

image Note: Your setup doesn’t have to be exactly like this, but this simplified setup will help you understand the concept.

For this tutorial, we will use an example provided by TheDevKing, as it’s straightforward and easy to understand.

The code in the localscript (blue script)

luaCopy code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local RemoteEvent = ReplicatedStorage:WaitForChild("TellServerRequestAndReturnTheRequestToGetRequest")

local PotentiallyAffectedByRequest = game.Workspace:FindFirstChild("PotentiallyAffectedByRequest")
if not PotentiallyAffectedByRequest then
    error("PotentiallyAffectedByRequest object not found in Workspace.")
end

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.Delete and not gameProcessedEvent then
        print("The local script must make a request to the remote event in order to communicate with the server to delete the part!")
        RemoteEvent:FireServer(PotentiallyAffectedByRequest)
    end
end)

The code in the serverscript (regular or grey script)

luaCopy code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("TellServerRequestAndReturnTheRequestToGetRequest")

RemoteEvent.OnServerEvent:Connect(function(player, part)
    part:Destroy()
end)

These scripts work together to delete a part.

The context of what these two do.

The serverscript context

  1. The serverscript receives the client’s request through the remote event. It runs on the server-side and listens for the remote event named “TellServerRequestAndReturnTheRequestToGetRequest” using RemoteEvent.OnServerEvent:Connect().
  2. When the remote event triggers, the serverscript receives the player who initiated the request and the part object that needs to be deleted. It then deletes the specified part using part:Destroy().

The localscript context

  1. The localscript runs on the client-side and is responsible for initiating the remote event when a specific input is detected.
  2. It waits for user input, and when the Delete key is pressed (input.KeyCode == Enum.KeyCode.Delete), it calls RemoteEvent:FireServer(PotentiallyAffectedByRequest) to trigger the remote event. It also includes the PotentiallyAffectedByRequest object as part of the request.

What happens when two players are in a server and one of them deletes the part using remote events

MORE CONTEXT

In Roblox, a server is the container for all clients. Each client represents a player in the game. When a player wants to affect other elements within the server (the container), remote events come into play. A client can initiate actions that affect other items within the container (the server). A localscript detects an event made by the client, where the client requests to communicate with the server in order to affect other elements within the container (the server).

Verbal visualization

Let’s use a verbal visualization to understand how the player communicates with the server:

The client (Player): “I want to delete a part, but as a client, I don’t have the authority to do so. I can delete it locally using a localscript, but only I can see the change. I really want other players to see that I deleted the part.”

Remote Event (The messenger between the client and the server): “Oh! The client has requested to delete the part from the server! I will relay the client’s demand to the server script.”

The serverscript (The recipient of the message): “The remote event has informed me that a client wants to delete a part, so others can see the change. I will delete it now.”

After this process, the part no longer exists for everyone in the game.

To further visualize this scenario, you can watch TheDevKing’s example for this tutorial: Remote Event Example.

Ending

Thank you so much for reading through this tutorial! I hope you have learned something new and interesting about using remote events in Roblox Lua scripting. Happy scripting!

The media I provided

  1. Please note that all the media provided in this tutorial, including links and images, are for educational purposes. I do not claim ownership of the linked videos and acknowledge that they are the property of their respective creators.

I hope these improvements and additions better align with your requirements. If you have any further questions, feel free to ask!

8 Likes

Although client>server remote events are explained in this post. There is still server>client and server>all clients missing. After all, the title does say Let's learn how to use remote events!, not something in the lines of Let's learn how to have client>server communication with remote events.

The roblox documentation already explain how remote events work in a better way (from my perspective) with more clear exemples.

I also think that you could add a mention about security checks, since client>server>? communitations can be abused by exploiters if they do not have safety checks.

3 Likes

Hey, thanks for your response, this is a post with TheDevKing’s remotevent setup. :>

1 Like

I’m fairly sure this doesn’t explain how RemoteEvents work in detail, you’re entirely missing the Server to Client part, which is a huge feature that everybody should know about, no matter what type of game they are making.

I will try to improve it, thanks. Btw there is a video link that explain server to client.

I don’t enjoy videos learning about functions and methods, since I can’t freely understand it, many others who see this may feel the same way.

In my opinion, this is an excellent explanation, you explain it in an excellent way and absolutely people can learn a lot from it. Well done.

1 Like

This one is more focused on the remote events, not client and server.

But… remote events are designed to make communication between client and server… do you not understand?

Yea felt lazy to explain that sorry, I will update it if I can.

Hey I changed the whole paragraph now ^^