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!
- 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)
- 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.
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
- 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()
. - 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
- The localscript runs on the client-side and is responsible for initiating the remote event when a specific input is detected.
- It waits for user input, and when the Delete key is pressed (
input.KeyCode == Enum.KeyCode.Delete
), it callsRemoteEvent:FireServer(PotentiallyAffectedByRequest)
to trigger the remote event. It also includes thePotentiallyAffectedByRequest
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
- 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!