• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Roblox How to use RemoteEvents in Roblox Studio (1 Viewer)

In this tutorial, I will go over:
  • How to detect when a user clicks a part using ClickDetector
  • How to send a request through RemoteEvent
  • How to read the request inside of another script

Please comment if I missed anything, and without further ado, let's begin!
---------------------------------------------------------------------------------


Setting up your workspace

To start, begin with a blank project (or an existing one, whichever you feel like), and place a part on your baseplate.

In the Explorer, navigate to Workspace -> (Name of the part you entered, default is "Part")
Annotation 2020-01-10 163136.png








Right click, and navigate to Insert Object -> Click Detector, and insert it.
Annotation 2020-01-10 163335.png















Now, right click on Workspace, and navigate to Insert Object -> RemoteEvent, and insert it.Annotation 2020-01-10 163721.png



















Then, right click on the RemoteEvent you just created, and navigate to Insert Object -> Script (not Local), and insert it.
Annotation 2020-01-10 164511.png














And finally, right click on StarterGui, and navigate to Insert Object -> LocalScript, and insert it.
Annotation 2020-01-10 164106.png











































Open the LocalScript you just created, and it's finally time to code!

---------------------------------------------------------------------------------

This next part is designed for you to actually learn Lua, so if you don't care for that, just copy-paste this into the LocalScript:

Lua:
local clickDetector = game.Workspace.Part:WaitForChild("ClickDetector")
clickDetector.MouseClick:Connect(function()
    game.Workspace.RemoteEvent:FireServer()
end)

---------------------------------------------------------------------------------

Programming the LocalScript

We need to start by declaring a variable, and we will do that by entering local clickDetector =. "local" means that the variable can only be accessed from that script and not others, and "clickDetector" is the name of the variable. Feel free to change that to whatever you want, but you will need to use it in the next step.

We will set this equal to game.Workspace.Part:WaitForChild("ClickDetector"). "game" is the folder that includes anything you can see in the explorer (and more!), "Workspace" is a child of "game", which we put the Part and RemoteEvent in earlier. "Part" is a child of Workspace, and it is the part that we entered the ClickDetector into. Change this to the name of your part. ":WaitForChild("
ClickDetector")" waits for the ClickDetector, a child of the Part we entered, to load in for the user and then functions as if we would just directly state "Part.ClickDetector". You can learn more about this here.

The whole variable should look something like this:
Lua:
local clickDetector = game.Workspace.Part:WaitForChild("ClickDetector")

Next, we need to detect if the user clicks the part. The code looks like this:
Lua:
clickDetector.MouseClick:Connect(function()

end)
The "end)" will be automatically produced for you.

"clickDetector", the variable we just set checks if your mouse clicked the Part, in "MouseClick". We continue this by entering ":Connect", which connects with the RemoteEvent's script to trigger the function we will put in it in just a moment. Then "(function()" declares that we want this event to be a function. Be careful not to close the outer parentheses, as that is what the parentheses after the automatically generated "end)" is for.

Inside the function, there will only be one line of code, and it is:
Lua:
game.Workspace.RemoteEvent:FireServer()

The only new thing in here is ":FireServer()". This is what sends the signal to the remote event to call the function.

That's it for the LocalScript!

---------------------------------------------------------------------------------
This next part is also designed for you to actually learn Lua, so if you don't care for this either, just copy-paste this into the Script:
Lua:
game.Workspace.RemoteEvent.OnServerEvent:Connect(function()

end)
---------------------------------------------------------------------------------

Programming the Script

Inside the script, we need to recieve the signal sent by the LocalScript we just programmed to do this. This will also be a ":Connect" function, and it looks like this:
Lua:
game.Workspace.RemoteEvent.OnServerEvent:Connect(function()

end)

The only new thing here is "OnServerEvent". This is what responds to "FireServer()" in the LocalScript.

From here, feel free to add your own code inside this, as there's nothing left to activate the RemoteEvent. Congratulations on surviving this tutorial!
---------------------------------------------------------------------------------

Video tutorial, with minor differences

---------------------------------------------------------------------------------
 
Last edited:

GavinW

Newcomer
Creator of RiscLua
Joined
Oct 21, 2020
Messages
41
Reaction score
17
Age
82
Location
UK
Website
www.wra1th.plus.com
I like your Avatar, Damian. Seeing that Lua is portable to so many platforms, would it not be useful to say in your tutorial what operating system you are using, together with any pertinent software? I have heard of Roblox, but never seen it in action.
 

damian

Newcomer
Joined
Jan 10, 2020
Messages
8
Reaction score
7
Location
Illinois
I like your Avatar, Damian. Seeing that Lua is portable to so many platforms, would it not be useful to say in your tutorial what operating system you are using, together with any pertinent software? I have heard of Roblox, but never seen it in action.
Roblox Studio (their development platform) is available on Windows and Mac (not Linux, unfortunately), and doesn't require any extra software. I don't even have Lua installed on my computer and it all still works.
 
Top