welcome back to my Roblox beginner scripting tutorial guide my name is balev and in this episode we'll be discussing about events now I want you to imagine that it's your birthday today uh you just had a long hard day of school and you're walking back home from the bus and you're about to open your front door and when you do open your front door you are greeted with a surprise birthday party that was hosted by your family and now they are coming over to you to give you hugs give you presents and enjoy the rest of the day specifically for you because you opened your front door that basically triggered the situation to happen now basically what I'm trying to get at here is the concept of events where things happen because other things have happened so that basically works the same way when we're using events inside of Roblox studio and that's what I'm going to be showing you how to do inside of this episode so what we're going to do on the right side is basically click our script that we created in the last episode disable it and then we are going to insert a new script inside of the workspace just like this I'm going to rename this to events and I'm going to hit enter and I'm going to delete this code so events is a pretty broad topic to talk about in Roblox studio and it can be very complicated depending on what kind of events you're working with so for the sake of this episode uh I'm going to be introducing to you two built-in functions that Roblox Studio has I'm going to be providing to you two built-in events that Roblox Studio has provided to us that I think are the most common ones used that I think you should be aware of um as for this tutorial so what we're going to do is go back to our script and there's going to be two of them but the first one I'm going to introduce to you is the trigger or the event of when a player joins into the server and this is called the player added event that lies within the players folder now it's very interesting because up until this point we've only worked with the workspace folder contained within the game data model but this time we're actually going to be using an event that's contained within the players folder inside of the game data model so what we're going to do is go back to our script and we are going to say game. players so we're not going to say workspace we're going to say players and then we're going to say player added and you can tell that this is an event with this lightning bolt symbol over here so I'm just going to auto correct this with uh player added and here's the interesting thing so we have our player added event but we need to be able to connect this event to a function that is going to run when this is triggered so basically what this is going to look like is we're actually going to put a colon symbol right here and then we are going to say connect just like this you can either say connect with an uppercase C or a lowercase C it doesn't really matter that much but I'm going to say uppercase C and then what we're going to do here is put in two parentheses so open and close parentheses just like this and then we're going to put a function within these parentheses so we're going to create a new function by saying function open and close parentheses and then we're going to go on this right side here which is in between these two parentheses and we're going to hit enter just like this and one final thing is we're going to be adding a parameter that Roblox has provided to us which is the player that has joined into the game for this event so we can call this whatever we want but I'm just going to say player just like this and so this is the structure of how we create events and how we make function that fires once the event has been triggered this is one way writing it but I'm going to show you the other way in just a little bit so what I'm going to do is write a print statement here by saying a new player has joined into the game and what's also interesting about this is we have the player itself like the player object being passed into this as a parameter so we can actually do things with this player and just for a simple example we can print out the player itself just by saying print open and close parentheses and we can just pass in the player directly and if we go into the game and hit play then what should happen is it's going to say a new player has joined into the game and then it's going to print out the name of my player which is brawl battle because I joined into the game and this fires every single time a new player joins into the game so that is one of the most common built-in events that Roblox Studio has for us that we can use so I'm just going to hit stop and what we're going to do now is I'm going to show you the other way of writing an event and this one should be more familiar with the way we write function and how it applies to an event so what I'm going to do is delete our code that we have here up until this point and we're going to create a function so we're going to say local function we can call this whatever we want we can say player added just like this and then inside of these parentheses we're going to pass in our player parameter just like this and then we're going to hit enter so we can do the exact same thing we just did in the last uh example we can say print a new player has joined and we can even print out the player here as well now we've created the function we still need to add our uh player added event down here by saying game. players. player added colon connect open and close parentheses now instead of writing a new function inside of here we can just simply pass in our player added function that we created on top of here previously so we can just say player added which is a reference to our player added function that we just cre created and it's automatically going to pass in the player argument just like that and this is another way we can write events and how we attach a function to these events when they get triggered so if you go into the game and hit play then it should basically say the exact same thing saying a new player has joined and it also printed out my username right down here so that is basically how we write events inside of our scripts and that is how we use the player added event but now I'm going to be showing you the second event that that is very commonly used and that is called touched so basically every single object or instance that you can see inside of this workspace like uh these parts and also the spawn location and this base plate they have a built-in function called touched which basically triggers if another part touches it now what this means is uh we can either have another part touch a part or we can have our player touch um the part as well because our player is also classified as a bunch of parts put together as well so this is very useful if we want to have a touch detection for if a player steps on a part and so that's what I'm going to be showing you how to do for the rest of this tutorial so what we can do is go into model and we're going to click on part so we're going to insert a new part inside the workspace and I'm going to go to the right side rename this part to touch part just like this and I'm also going to go down here and I'm going to check this property called anchored now basically what anchored is is it's a property that allows us to hold a part in place so that it's not affected by gravity so what I'm going to do is basically click on this move tool and I'm going to move it up here so that um once it's up here it's going to stay in place and it's not going to drop down due to gravity since we have this anchored property enabled so now that we have this let's go back to our script and I'm actually going to delete everything we've had up until this point so I'm going to hit contr a and delete and I'm first going to make reference to our touch part so I'm going to say local touch part equals game. workpace do touch part just like this and now what I'm going to do is say touch part. touched so this is an event that's contained within our touch part and then we're going to say colon connect open and close parentheses we're going to create a new function so we're going to say function open to close parentheses again and we're going to pass in the other part that touched this part so we're going to say other part just like this and then in between here we're going to hit enter and so now what we can do is we can basically print out um the other part that touched this part in specific which is the touch part now specifically what we can do is print out the name of this other part so we can say dotame just like this so we can see what's touching this part inside of studio so if we go into the game uh hit test and hit play then what we should see uh is first of all the part is floating in midair because we enabled the anchored property so if we touch it what we should see is a bunch of parts touching this touch part um and these are all referring to parts that are contained within my character so we have things like the upper right arm uh the head upper torso left upper arm and things like that so we can keep on touching this and it's going to basically print out out every part that's touching this part that's over here now because our character makes up many different parts it's constantly firing in the output uh all of these different parts that's touching this part so what we can actually do is if we want to have let's say uh the player touch this part one time and then wait a certain amount of time before we're allowed to touch it again we can use what's called debouncing which is basically what allows us to have a coold down for this touch and I'm going to show you what this looks like really quickly so I'm gonna hit stop and I'm going to go back to our script and what we're going to do is create a Boolean value that basically says whether this part is currently being touched right now so what we're going to do is say local part is touched equals false So currently the part is not being touched and so basically what we're going to do is drop a line at the start here and we're going to check if the part is currently being touched or not okay and so what we're going to do at the start here is we're going to write an if statement by saying if part is touched equals false so basically if the part is not being touched then we can trigger whatever is going to be inside of here by saying uh then like this and then we're going to hit enter then we're going to move our print statement inside of here and what we're going to do at the start here is we're going to say part is touched equals true now that uh we know that part is touched as false we're going to set this equal to true and then we're going to make the print statement down here now to add a cool down what we're going to do is put in a task. weight for a certain number of time uh we can put in however many number of seconds we can put in here we can say let's say 2 seconds and then we're going to say part is touched is back equal to false just like this and this is basically how we add a debounce or a check um or a cool down to our touched event just like this we first check if nothing is touching the part then we're going to say the part is being touched and then we're going to wait 2 seconds and then we are going to say that the part is no longer being touched so we can touch it again so if we go into the game hit play then what what should happen is if we touch this part then it's only going to say one thing has been touched and it's not going to repeatedly um print out or execute this event until we wait 2 seconds so we touch it again it's going to say something else has touched it and then it's just going to do this after we wait for the cool down and that is basically how we add a cool down to these event calls and that is by using debounce so I hope you learned a lot from this episode with event those are basically the basics of using events and now for today's learning objective what I want you to do is continue to use uh this touched event or use the player added event so that you can do more stuff with either the player or you can do more stuff with these parts you can maybe even change the color of these parts if you step on it and make it go back after a certain period of time there's a lot of possibilities you can do with this so I encourage you to do that and once you do that I want you to go down to the comments so that you can paste your code so that other people can see what you're doing that you feel comfortable sharing and with that being said I hope you enjoyed this episode and I will see you in the next one take care