welcome back to my Roblox beginner scripting tutorial guide my name is balev and in this episode we'll be discussing about functions now once again this is going to be another very fundamental Concept in scripting that you will be using a lot inside of your scripts so with that being said what we're going to do on the right side first is we're going to disable our property script that we created in the last episode so I'm going to select it and then uh click on disable and what I'm going to do is hit the plus sign
next to workspace search up a script so we're going to insert one of these and we're going to rename the script so I'm going to right click it and rename we're going to call this one functions enter just like this and now we have our new script for our functions so what I'm going to do is delete this and before I introduce to you what a function is let me present with you a scenario so let's say I wanted to take the game's base plate and repeatedly change one of its properties like let's say the
transparency for example what I'm going to do is go to our script and we're first going to make reference to this base plate like we did in the last episode so what I'm going to do is say local and then we're going to put in a um a variable name and this can be whatever you want but I'm just going to say my Bas plate equals game. workpace dobas plate like this and I'm going to change the transparency property of the base plate so I'm going to say my base plate. trans trans parency equals 1
now if I wanted to copy this and paste it um two more times then what I'm going to do is Select this line and uh we can actually copy it uh by right clicking and then hitting copy now as a shortcut what we can do is uh type in control like hold down control and then C for cat and then we drop a line down here and then we just paste it by by holding down control and typing V for violin just like this and it's going to paste it in just like that so then
we can um replace this with 0.5 and we can drop one more line uh hit control V and then we can paste this to be zero so these are the three lines of code that we have over here but what if I presented to you a scenario where we basically copied these three lines so we copi this by hitting control+ C and then dropping down two lines by hitting control+ V now we have six lines of code where we change the transparency and if I do this again then we have nine and if we keep
on going like this then we're going to have a lot of transparency um changes to this base plate so we're using three lines of code to change uh the transparency of this base plate for each block that we do here but what if I told you there's a much better way to do this and a much more efficient way to do this and that is by using what's called a function now basically what a function is is it's a piece of code that basically uh contains m multiple lines of code that executes just by calling
it in one line it may not make sense to you unless I show it to you so what I'm going to do is repeatedly press this undo button um so that we can backtrack now we can also hold control Z to do the exact same thing Z as in zebra and now we are back here with three lines of code so what I'm going to do is after we make reference so what I'm going to do is after we make reference to our base plate what I'm going to what I'm going to do is drop
two lines down here and we have and we have to be able to create our function so the way we do that is by first specifying the keyword local now once again I will explain this in a future episode but bear with me as we type out this function we're going to hit space and we're going to put in the keyword function in all lowercase so that we know that this is a function that we're creating and then we can name this function whatever we want this to be so we can call this let's say
change transparency like this and what we need to do is add in the open and close parentheses just like this and now uh we're going to select the right side so we're going to click on this and then we're going to hit enter now as you can see Roblox has automatically added in this end keyword for us and there's like this little indentation that's going on right over here so if we type something then it's going to be indented um a little bit so that it it'll show that whatever we type inside of here is
going to be inside of this change transparency function now what we can do from here on out is basically take these um pieces of code these three lines and I'm actually going to uh cut this line of code so what I'm going to do is hit control X so that it's saved to our clipboard and I'm going to go over here and I'm going to hit contrl V to paste the code inside of this function so now our three lines of code is inside of this change transparency function rather than it being out here so
now this should work pretty much if we go into the game and hit play then we should see the change right well no that's actually incorrect we don't see the change to the base plate and that's because we only created the function but now we actually need to call this function so what we need to do is hit stop and go back to our functions script what we need to do is actually call this change transparency function and we simply just need to say change transparency open to close parentheses just like this so basically what
we have going on right here we just simply paste it down here just like this now in order for us to really see this change what we need to do is change this further because by default the base Plate's transparency already is zero so what we can do is basically copy this line so crl C and then contrl V down here and I'm also going to copy this line so I'm going to hit contrl C and then uh I'm going to hit enter and then hit contrl V so now we have these five lines of
code right here let's go into the game hit play and now what we should see in the base plate is that it is indeed transparent after after going through all these different lines of codee so now we can basically repeatedly call the same change transparency function by going down here and then hitting change transparency just like this and it'll basically act the same way as if we were to copy these five lines of code drop two lines and then do it just like this so this is the equivalent of this uh over here and this
is a much better way of writing our scripts and it's definitely going to help us with code organization and optimization for the future when we work on more complicated scripts so now what I'm going to do is make another function example so what I'm going to do is basically select everything and then delete it uh a shortcut we can use is hitting control a as an apple uh so that we can select everything that's inside of the script and then we can hit backspace to delete it so now what we're going to do is make
another function I'm just going to say local function uh let's say print ABC open and close parentheses just like this and I'm going to hit enter so I'm just going to make three print statements by saying print open close parentheses uh in quotations I'm going to say a and then I'm going to make another print statement so I'm just going to hit enter to uh correct uh to autocomplete this and I'm going to say B and then finally print C just like this and now we can basically print out um each of these messages just
by calling the print ABC function just like this and we can basically do the same thing down here print ABC open close parentheses just like like this and we can do it again one more time just because why not ABC open close parentheses and now if we go into the game and hit play then what we should see in the output is ABC being repeated three different times because we called ABC one time which is going to go through ABC and then it's going to uh do the same thing again with ABC and then finally
do it one last time which is a b and c so I hope you're able to understand how these functions work and how this is going to be useful inside a future episodes so that's pretty much going to be it for the introduction to functions what I want you to do for today's learning objective is to create more functions that do different things like let's say if we were to take um one of these parts and if we were to let's say change the material to something that's different every single time uh or if we
were to basically change one of these other properties to be something different then you can go ahead and do that and once you do that then I want you to go down to the comment section and paste in your code so that other people can see what you've been doing and so that's basically going to conclude this episode I hope you enjoyed it and I will see you in the next one take care