You can access the full course here: Explore Roblox Scripting with Lua
Intro to Events
In this lesson, we’re going to learn about events in Roblox.
An event allows other objects to get notified about an action that happened in the game. For example, we can be notified of when a player touched a particular object by listening to the Touched event of that object.
To illustrate this, let’s create an object that changes color when the player walks into it.
Add a new part to the game view and rename it to TouchButton. Change its Size to be ‘3, 3, 3‘ in the Properties window, then change the color to red and the material to be Neon (in the Home tab):
Back to the Properties window of our TouchButton part, enable ‘Anchored‘ and disable ‘CanCollide‘ as we want the player to be able to walk through our cube:
Also, move it a bit up with the Move tool (in the Home tab).
Now that we have our button, let’s make it turn green when we touch it.
Create a Script for our TouchButton and call it ‘TouchButton‘ as well.
We start by accessing the part (which is the parent of our script) and connecting to the Touched event. We then pass in a function with the part that touched our button as a parameter. If we press Play and check the console, we’ll see that the message has been printed out multiple times. That happens because our player is made up of several different parts, such as the head, hands, feet, etc.
To make our button change to green, let’s assign it a new color as follows:
Lastly, let’s have a look at an event that gets called when a player joins the game.
Here, we do ‘game.Players‘ to access the Players service (visible in our Explorer window) and then we listen to the PlayerAdded event.
Playing the game, we should see our own name printed along with the message in the Output window.
Hey, everyone. In this lesson, we are going to be looking at events inside of Roblox Studio. So first of all, what is an event?
Well basically, an event is something that allows other objects to get notified, okay? For example, if the player touches a certain object, what we can do is connect to that event, the touched event, and be notified when the player has touched that object, okay?
It’s similar to calling a function, but a lot more versatile and allows us to basically plug in as many things as we want, okay? So, for example, we could have a large field of hundreds of cubes, for example.
And when the player, for example, clicks a button, all those cubes could change to green, okay? That’s what an event would allow us to do. So, what we’re gonna do is go ahead and create ourselves a little button. And what this is gonna do is turn green when we go inside of it, okay?
When we walk into the object, it’s gonna change color. So, for this, I’m just gonna go ahead and create ourselves a part right here. Let’s rename this one to our touch button. (keyboard clacking) I’m going to change this size, where is that, that is over here.
I’m gonna change the size to be, let’s just say, three by three by three. So, that is three by three cube right here. Let’s go ahead change the color to be red by default and we’ll change the material to be neon, so it is glowing.
While we’re at it, let’s also go enable Anchored, and disable CanCollide because we want the player to be able to walk through this object. All right, maybe move it up a tiny bit like so, and there we go. So now, let’s make this object turn green when we touch it.
So, I’m gonna attach to it a script right here. This script, I’m gonna rename it to be touch button, just like with our object. And here we go. So, first things first, we need to get our actual part, okay? The touch button part that we’re going to be checking. So, I’ll create a local variable called parts, which equals script, oops, script.parent.
The script right here, the parent object is going to be the touch button. That is what we are accessing. So, now that we have referenced to this touch button, what we can do is connect the touched event, okay? So, how do we do this?
Well, if we type down part, then put a dot, you’ll see this list pops up with all the various different things we can look at. And if you scroll down, you’ll see that most of them have this blue little cube next to it. This is a property.
If you look into the properties tab on the bottom right, basically, this is all of the fields that we can modify. You know, the name, the color, the opacity, the parent object, the size, the position. But if you scroll down a bit more, you’ll start to see these lightning bolts, okay?
Stuff like child added, changed, touched, touch ended, and these are all events, okay? So, these lightning bolts are events that we can subscribe to or connect to. And we will basically be listening for when these events are called. And when they are called, we’ll be notified, so we can do whatever we want to do with it.
The event we want to do is go down and find touched, okay? Now, touched is an event that every part in Roblox has, okay? Every part in Roblox that can be touched by a player or entered by a player has this touched event. And this touched event gets cold or invoked once a player inters or touches a part, okay?
And what we’re doing here is we are gonna connect to it. So, the single colon connect and inside of the parentheses, here what we need to do is create a new function that is gonna be cold, okay? So we’re gonna create a function here and we need one permanent to be sent over.
And the object that’s gonna be sent to over is going to be the touched part, okay? So basically whatever part touched us, that’s gonna be sent over, in our case, this is going to be the player when the player touches the part, but doesn’t have to be the player.
It could also be a projectile, could be a rolling ball that rolls through this cube. This event is still gonna be invoke. So, when the player, or when anything touches this object, this touched event is gonna invoke. And since we are connected to it, this card is gonna run inside of here.
So what we can do is print to the output and let’s just say, we’ll go, touch button was touched, okay? We can press play and test it out. So, we’ve got our touch button over here, we’ve got changing our color cubes from the previous lessons. Let’s go up to this cube, and if we look in the console, there we go.
A touch button was touched 16 times. And that is because our player is made up of a number of different parts. We’ve got our hands, feet, our legs, our torso our head, our hair. So that’s why it’s being called multiple times, okay? But as you can see, it is detecting when we are touching this cube right here, as you can see, it goes up, it goes up again.
And events are very powerful in this way, as well as these pre-made events that these objects have, you can also go ahead and create your own. Now, in the next lesson, we’ll be going over actually creating, unique and custom events.
But for now, let’s go and change this color to red. So how do we do that? Well, what we’re gonna do is we’re gonna remove this print line of code right here, and we are going to make the color green. So, for that, we can go part.color equals color3.new.
And if you’re a member of color3 requires three different parameters, a red, green and blue value. Now, since we’ve been able to make this color green, we’re gonna give it a zero for the red, for the green, we’re gonna give it a one and a zero for the blue. So this is gonna be an entirely blue or entirely green button, okay?
So that we can save that, press play. And let’s see if it turns green when we touch it. So, let’s go over here, and there we go, it is turned green. So that is how you can use events for these customer or buttons. You could even maybe have checkpoints as well.
So when the player jumps and lands on a platform, you can register, change place team to be linked to that checkpoint. There’s a wide range of different things you could do. Now as well as parts having events, pretty much everything has events inside of Roblox Studio.
For example, we can have an event that gets cold once a player joins the game, or we could have an event that gets cold once a lot is turned on, okay? So let’s have a look at an event that gets cold when a player joins the game.
So to do this, what we need to do is first of all, access our players service right here, which basically manages all of the players in the game. And to get that we can go game.players, then go .playeradded, as you can see right here, it is an event, colon connect, and we need to create a function with a parameter of the player that is joining, okay?
So, game.players accessing this service right here. Then we are looking at the player added event and we are connecting to that event. So when a brand new player joins the game that play added event is gonna be cold, and that means that this card is gonna be ran.
Now, let’s just say, we want to go ahead and print to the console, the player has joined the game, okay? So for that, we could go print and then we could go player.name, add two dots to basically combine this with another string.
And then in quotation marks, we can put a space and go has joined the game. And okay, just like that. So now when we go ahead, press a five to play the game, we should see down here in the output, it says there we go, I have joined the game.
Okay, so we’ve got all this setup, we had to look at events, here. I’m just gonna remove this for nuisance. It’s good to really put code that only relates to that script in that script, so changing the touch button color to green.
In the next lesson, we’ll be looking at creating some custom events and then connecting those events up. So thanks for watching, and I’ll see you all in the next lesson.
Interested in continuing? Check out our all-access plan which includes 250+ courses, guided curriculums, new courses monthly, access to expert course mentors, and more!