Learn to Script - Your First Game #1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello welcome back folks so if you watched the last episode you'll have seen me show you the basics of studio and we made ourselves this little football pitch here if you want your place to look the same as mine i'll be putting a link down the description where you can download the place files for each episode in this series as we go along but moving on as today is where things really begin because in this episode we're going to start learning the very first basics of scripting as we've previously seen you can make lots of things with just building but if you want to make a more exciting game you're going to need something called a script now in case you're wondering what exactly a script is it's just a way of writing instructions that can then be carried out by the computer this is useful because then we can write instructions as things to happen while the game is running okay so let's get scripting but before we do we're going to need one more important item to help us now i've already used the explore and the properties menus but we're going to need one more so let's head over to the view tab i'm going to select output and when we do this little window is going to pop up now you sometimes see little messages from the system when you save the game here so if i hit control s you'll see episode 1 was saved to file so you see things like this here which aren't really that interesting but what it is very useful for is allowing our scripts to communicate with us what do i mean by that well let's add in our first script so head over to the model tab and over on the right here you see the script local script and module script we're not interested in these last two so you can just add in a regular script and when you do you'll notice it already has some text inside it so every script comes with a sort of template code if you will with the words print and then a sort of bracket quotation mark and hello world what's all this about well it's an instruction or a command or a bit of code and i'll tell you what we can do we can just run the game and see what happens so select the drop down here and we don't want to play it we're just gonna run okay so hit run and when we do so we should see down the output the message hello world so that's what print does it takes a message and it puts it down in the output for us so whatever within these quotation marks notice how it's a purple as well so we could type whatever message we wanted we could put um hello youtube for example and if we ran that now we'd see hello youtube down the output now it does need to be within the quotation marks so if we got rid of them it would change from purple to black which is telling us something's not quite right already and we've got a red underline so there's two signs we could be going wrong and if we try to run it now we would actually see some red text appear so this is an error and it tells us that something wrong with our script now this is why the output is really useful because if you ever make a mistake you will see it here because it's saying that's not valid code and it's not going to run so we'll stop the game head back to our script now as well as just text you can also put numbers in the print so you can put 10 and you don't need the double quotes when you're using numbers so they can just work on their own i can print out the number 10 and we can also do calculations here so i could say 10 plus 25 and i should see 35 down the output when i run it yeah 35 and i could do all kinds of calculations if i wanted i could do 10 times 25 divided by 1.5 and then i would see that's 166.66 recurring by the looks of it so i could do all sorts of things with the print command but we're not going to worry too much with that for now okay so we'll delete that and instead i want to make some changes to the game world now so let's say our ball over here now currently it's set to unanchored so we can drag it around and we can see it's got all these properties and if we wanted to change them we could do so just in the edit menu here but we wanted to happen while the game is running well we need to do it from a script now we can't just type something like make ball invisible clearly that's not going to work it doesn't know what make means it doesn't know what any of this means really so we need a way to tell the computer where to find ball first of all now we'll notice that ball is within side the workspace so if i hit this little arrow to collapse everything you can see everything within workspace hidden so we know everything is inside of workspace now thankfully workspace is actually what's called a variable a global variable and so we can just access it so if i type in workspace you'll see it sort of highlights in blue you might be able to notice that and then we can access things inside of it by using the dot a full stop or a period so i press dot and then i can access the index of everything inside workspace so i get this little auto suggestion prompt box and i can select anything from within that but i can just type ball as well now it's really important when you're typing these out it is case sensitive so if i try to type in ball all in capitals like that that is not the same as ball ball in all capitals doesn't exist only ball like that exists because that's how i've spelt it over my explorer okay so let's now change one of its properties how about we make it invisible okay so that would be the transparency property so if i was just doing it within studio i could change that to one and it would be invisible so let's do it from our script now ball and then we'll index that so use the dot and then we'll type transparency we can press enter and it will auto complete for us and then we need to set that equal to one so use the equals symbol and then we'll type one so if we just take a quick look again we'll notice the ball is currently fully visible but when we run the game so go to test and run again we'll notice the ball is invisible it's still there but it's just set the transparency to one and we stop the game well it resets so there we go our first script affecting the game now we could make multiple changes if we wanted we could even set it to one and then we could set it to zero again how about we do that because scripts run one line and then the other so it will always do what's on top first and then the second line now if we try to run this we'll notice oh nothing's changed transparency is still zero i wanted it to flash between them both well the reason for this is because script runs so quickly that it's doing both of these lines just within a millisecond if we want more of a delay what we can use is the weight command okay so we'll add another line in here we'll put type in weight and we want an opening bracket and a closed bracket like that and then we can put a number in there so i'm going to put one which means we want to wait for one second before going to the next line okay so now let's run the game we'll watch this it should make it invisible wait one second and then appear again and if we wanted we could repeat this yet again so we could add another weight one and then we could set it to fully transparent again i'm copying pasting these lines to make it a bit easier we could copy them both again and then we can set it to fully opaque again we could run that and it's going to be invisible invisible visible and there we go a little flash now how about we change a another property now how about we add in a wall we're going to give our team a bit of an advantage here we're going to cheat really so again add in a wall over here this is our goal i'm going to fill in the wall so the opposition team can't score okay and instead of putting a script inside workspace we're going to put a script inside of this part now so we're going to click that little plus and we're going to add in a script inside of it now instead of saying workspace.whatever we can access this from a different way so we can say from the script instead script is another global keyword we can use so script dot will get the index of the script and then we can access a property called the parent now the parent or attribute i should say um is whatever it's inside of so the script is inside of part so the part is its parent and the parent of this part well that's workspace so we're going the other way up we're going up the tree now whereas before we were going down the tree if that makes sense so now we've got the parent which is part then we can access the properties of it and we could set the properties say let's change the material so the material currently it's set to brick and we can set this to i don't know let's look for a good property we could change it to oil for example so i can't just write foil because remember it's text so i need to do it within those double quotes again like that and it will turn purple so now when i run the game we should hopefully see the wall has changed it's now foil and you can also see that both was flashing at the same time so we can have multiple scripts doing different things in our game and they're all doing exactly as we told them to now if any of these things haven't worked for you it's possible you've got a typo same for example in here if i hadn't spelt this right then i should be seeing an error down the output when it gets to that line so there you go oh line seven transparent is not a valid member of part okay because i'd spelt it wrong so we could change that back and then here i could have an hour if i hadn't put it in the double quotes so these are the things you gotta watch out for and make sure you're keeping an eye on your output now i don't want to overwhelm you too much at this stage so we're probably going to leave it there for this episode hopefully that's got you familiar with the idea of changing the game's properties via a script rather than just via the properties window in the next episode we're going to look at how we can introduce some logic to make our games and our scripts a little bit more interesting but until then thank you very much watching and i'll see you next time see you later you
Info
Channel: GnomeCode
Views: 98,121
Rating: undefined out of 5
Keywords: roblox, studio, roblox studio, noob, learn to script, learn to build, absolute beginner, beginners guide, fundamentals, how to make anything, roblox building, gnomecode, game dev, tutorial, properties, print command, script, lua, luau, programming, coding, how to make a game, make a roblox game, roblox lua tutorial
Id: PEhxPo5Pct8
Channel Id: undefined
Length: 11min 46sec (706 seconds)
Published: Mon Dec 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.