Events - Roblox Scripting Tutorials 2021

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to your fourth roblox scripting tutorial in this video we're going to be looking at events so events detect when something happens in the game such as when a new player joins or when an object twitches apart when an action occurs that can trigger one of these events we say that the event is being fired now events are coded by roadblocks behind the scenes to detect whenever these actions happen so we don't have to worry about how the detection works or writing any code to deal with that what you do need to know though is how to run code when an event is fired because this is how we make our games interactive this is how we make code that kills a player when they step on a lava brick for example in the last video we looked at functions functions allow us to write code which won't execute straight away when the script starts running meaning we can execute it whenever we want later on in the script and when we set up an event in our scripts we provide it with a function and we tell the script that we want this function to run whenever a certain event is fired so any code that we want to execute so like i said in the example before we could write some code which um removes players health we can put that in a function and we can set it up to run that function whenever a player steps on this brick for example and we can use one of roblox's inbuilt events which they've already coded to detect when someone steps on this brick and then when they do that we can tell the script to run a function and we could have a function that will kill them by removing their health so this process is known as connecting the event to the function we're going to be telling the script what we want to do when the event fires now we don't know how to do this yet so i'm going to show you how to set up an event there's a lot of events in roblox there's a set list we can't just make up an event by ourselves well we can but that's a completely different video which we'll be getting onto later down the line but for now all objects in roblox as they have their own list of properties they also have their own list of events and to find that list we're going to go to the view tab and we're going to click on object browser now on the left side here we have a list of all of the different objects that are available to us in roblox and they're named by their class name now obviously we have a part in the workspace it's called test part but its class name is a part because it's still apart so we're gonna find our part and now on the right side we have a list of all of this all of this parts functions so a part can have functions we'll get onto that in a later video they are the pink icons they have properties all of these blue icons are the properties such as brick color can collide anchored for example but we also have these lightning bolts and these are our events now like i said these are created by roblox and there's only a set list of events so in this case we're going to be looking at the touched events now this event will trigger whenever a player steps on a part and we can set up this event to trigger a function when it happens so to set it up very easy we're going to be using this touched event so let's go to our script let's just delete this code because we're going to be doing some events and firstly like when we were looking at properties we firstly had to reference the object so in this case we're going to say game dot workspace dot test part okay we have now got our objects and we are ready to select the event which we want to trigger a function so we do a dot and you can see we've got our list of properties now if we scroll down through this list you can see we get to our events and we want to use the touched event so whenever somebody touches this part we are going to connect the event to a function we don't have a function right now so let's create a function let's just leave this here for now i know there's a red line underneath it but don't worry but let's create a function okay that is going to what should we do let's kill a player okay so we've given our function a name let's put in some brackets and let's add in the end here so now any code that goes inside this function will run whenever our event is fired because we're going to set up that connection in a minute so let's not worry about the code that goes in this function for now but imagine we've got our function complete with our code to kill the player we can now say colon connect and then we'll do a pair of brackets and then what we want to do is we want to put the name of our function in here so what we're saying is when a player steps on this part we're going to can we're connecting this event to this function and we're saying run this function whenever somebody steps on this part now if i was to put a print in here to say let's kill the player because we haven't got into oh if we just uh do some code to kill the player it's going to add a little bit more time so for now let's just do a print statement let's open up the output and let's join the game now if i step on this part you can see it says let's kill the player and you can see i step off and it stops printing okay you can see it's printed 10 times i'll get on to that in a later video that's because the event is registering for every single body part so it could be registering for my left foot right foot left leg for example but if we step on it again you can see it keeps printing out let's kill the player so every single time i step on that part it is now running my function and yes i could have some code in here which kills the player if you really wanted to but you get the idea okay you may notice that when we wrote in the function name here when we went to call the function we didn't have any brackets and that's because when we are putting the function name in these brackets we can't pass any parameters to them but what happens is the event automatically passes some pro some arguments to the function uh with some data which it may have collected from the event so for example what part actually touched the brick and we'll get onto that a little bit later on but events fire every single time and action happens so in this case it fired 29 times not just once so don't think that you know just because an event has fired one time it's not going to fire anymore it can fire for as many times as it likes as long as the action keeps happening now this isn't the most common method of writing an event function because in this example we have predefined our function normally people will define their function inside the connect statement so let me just get rid of this function here and instead what i'm going to do is i'm going to remove kill player from the brackets but i'm going to just expand the lines here uh give a bit of a gap between the two brackets or parentheses here and then inside of here i can write a function this is an anonymous function it's a function without a name whenever we do an event function like this where we define it inside of the connect brackets we don't give it a name it's an anonymous function so we just put our function in here and then we can write some code such as use steps on the part okay now this will do the same thing it will print whenever we step on the part it will run this function the only difference is like i said there's no name to the function and this function we don't call it we just have to put function end and it will run as soon as you step on the part now the the the best way to set this up when you write it is like this okay so you have your connect brackets you put your function in here with your two brackets and then you end it with a closing bracket now this is this is like the this is a common practice how events are set up how event functions set up they're much quicker to write out they take up less lines and they're my preferred method in my opinion and just to show you if we run the game here it's going to uh it's going to do the exact same thing if we step on the part you see it says you stepped on the part so a simple way to remember how to write an event firstly you're referencing the part then you're getting the event which you want to use then you're connecting it to a function and then you put your code that you want to run within that function there now i said earlier about parameters and arguments so when an event fires often they will automatically send some information to our function as arguments such as the player who joined the game or the thing the object that touched our part now we can see these parameters in the object browser so if we go to our touch event here you can see inside these parentheses here it's got one parameter and that is the other part so the other part that will have touched our parts which we've got the event for so it sounds a little bit confusing but our parameter then we just give it a name so let's just say um the part that touched the brick okay i've made it a long um parameter name to help with the explanation so you can call your promise or whatever you like but like i said in the previous video with functions you have to get the order correct so the event is telling us the order so if we had um two parameters for example like in this one here ancestry changed we've got two uh parameters one for the child one for the parent so the child will come first the parent will come second so you'd have to write them in order so you'd have to give your names to your parameters in order so it could be the child the parents but if you got them the wrong one way around then you would you'd mix it up so we're going to use the other part that touch the brick and the event automatically passes the arguments to us so we don't have to worry about passing that argument to the parameter like we did in the previous video we'd have to worry about that the event is going to pass it to our function for us we're just putting our parameter in because that's what the um object browser has told us to do it's as if the script is calling the function for us and adds the arguments so we can access them in our function by adding these parameters in order so i could then print that the part that touch the brick dot name has touched the part okay and we play the game and step on the brick and you can say look right lower leg has touched the part so that would be this part of my body or right upper leg has touched the part so i can i can touch the brick at different with different parts of my body so it's printing that each time because every time the brick gets touched roblox is passing the part that touched the brick to my function and then we can use that to do things in our code so you know if a player joins the game we can use the player added function so if we go to um players game.players is a service which deals with players in the game here we go players and it's got an event called player added with it with a parameter of the player who joined the game and whenever it says instance that means it's an object so we know it's going to be an object in the game so we could say game dot players dot player added we're referencing the uh the service or the object we're then getting the event called player added and then we connect it to a function so let's write a function in here an anonymous function because it has no name just has brackets on the end of it and then we could have our parameter called player you can call it whatever you like so it could be called the player and then i could say print the player.name has joined the game and i say dot name because the player is an object it's going to be my player and so it will have a name property now let's join the game and straight away it should say alvin blocks has joined the game so i'm sure you can see the power of events they can detect when things happen in the game and we can run code to do things so for example you know a practical example if you wanted to add a leaderboard add some a cash value to a player whenever they join to show that their leaderboard you could do that whenever they join and you could use the player a parameter to access that individual player and to add values or currency or leaderboards to their player now it's possible to disconnect and have events inside events essentially nesting them but we will look at this in a future video you don't have to worry about those right now we need to learn a few other topics before we get into that but that'll be fine but it's where you define the event listener which matters so this is called an event listener because it's listening out waiting for the event to happen now if i was to add a wait 10 seconds here it would not detect that i joined the game because i would have joined the game before the event listener was set up you can see it hasn't printed out alvin blocks has joined the game that is because it will only start listening for new players joining the game or whatever the event is once it's been set up and we set it up after 10 seconds so it's missed me joining the game so that is why when you have a player added event you want to have at the top of your script because you want it to start listening when the script starts running anyway that's functions and events i hope this video was useful if it was drop a like don't forget to subscribe share it with someone you know and i'll see you in the next one it's been really fun making this video and we're gonna have a lot more fun with events in future videos because they are a game changer for building roblox games thanks for watching
Info
Channel: AlvinBlox
Views: 109,195
Rating: undefined out of 5
Keywords: roblox, alvinblox, roblox scripting, roblox events, roblox connect, connect:function(), touched event roblox
Id: IcASLddinOk
Channel Id: undefined
Length: 14min 20sec (860 seconds)
Published: Sun Mar 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.