How-to: Lua Coding Introduction (Hour of Code Pt. 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everyone URIs mixed sparks here today we're gonna do a little lesson series on scripting and coding and we're going to learn how to make a couple cool features for a platforming game or obvious assault often called in roblox and we're going to start off at the very beginning kind of very basics of coding and scripting so if you've already done this sort of stuff before might be review but you know please feel free to stick around you might learn stuff and because we're gonna do some really cool interesting things today so let's go ahead and dive in so what is scripting and coding well to put it really really simply coding is just giving a computer a series of instructions telling it some special things to do and it's true Annelle all kinds of programming even in game making that's what we'll do today so I'm in a roblox studio I'm going to up in a couple extra windows you may already have these open by just wanna make sure that they're here I want the Explorer the properties in the output and I'm just gonna base a big MP baseplate world nothing fancy nothing to distract me and having these windows up it's just kind of helpful you'll you might see why later let's get started with the script so I'm going to right click on server script service down to insert object and click script and clear out the stuff that's here now scripts are a special type of object in roblox they can live in a lot of places most often you'll see them either in server script service they might be in workspace which is where most of your game world lives we'll talk we'll discuss where you might want to put your scripts later on for now server script service is nice just kind of a convenient place to put all of your scripts they all run with the game starts and it's just a nice way to keep it organized and so when you're in a script you'll notice that there it looks like a text editor because that's what it is it has a whole bunch of luck they'll have to be a whole bunch of lines of text and what the computer is going to do is it's going to go through each line in a script and try to do whatever it says is set on that line so for instance I can start off by saying print hello world and this is a special command that tells the computer to print out or the program to print out this message you'll notice that there's a couple special things here one I use the special word print it's all in lowercase no no capital letters I have these parentheses to open and close which surround the message I want to say and you'll note if we click play solo to run our game down here in the output it prints out the message and to get back and just have to click reset I notice remember how I said the computer is gonna run these one line at a time I can print out something else doesn't have to necessarily be a sentence I could print out the number five that's why not fives a cool number if I run this game said hello world because that was the first line and then went to the next one it said print out five just as things one at a time that's really all there is to it now we don't have to just print out words and numbers that's all fine and good but we want to probably look at some interesting parts of our game like for instance I can see that the base plate here is this gray this kind of dark stone gray I think it is well let's say I couldn't see that but I wanted the script to tell us what color it was well we can print that out too so I'm going to say print might open and close parentheses and what I'm interested in printing is the color of the base plate here you can see it in the workspace now how do we say to print out a property notice how dark stone gray is brick color of the base plate and that's what we want to print out so how do we say that we want to print out yes well we need to be very specific and tell the computer tell the program what exactly we want now when you're printing out something or you're manipulating something in the game rather when you're referring to something specific in the game something you see here in the Explorer you always start off the word game and now we have to tell the program where the thing we're interested lives well it's in workspace good thing this is like a big file folder and the base plate is inside the workspace can just say workspace dot game that workspace stop base plate when you see these dots that basically just means kind of like a file structure you're going into the next one so we start off with game which is kind of the overall the kind of thing that cap sleeves everything and we say go into workspace which is this right here let me say go into the base plate which is this object that's just underneath workspace so this just refers to the object itself just this brick if we want to see one of the properties we want to print it out we just keep going with what we've been doing we say dot again in this case let's say brick color and now we could just hit play solo and we got our hello world or five and it says dark stone gray and note that's exactly what is here if we changed the base plate color let's say we may have blue they play solo now it says hello world five right blue neat now there's a lot more descripting than just printing stuff out that's useful for alerting information so you can see what's going on in your game but it's not really it's very passive it's not really doing anything what if we wanted to change something like for instance we have a transparency property on our base plate it can be anywhere between zero and one if it's zero that means that it's completely opaque you can it looks like a solid object it's one it's completely invisible it was somewhere in between is little translucent like glass let's try that out let's see if we can change that with our script so I'm going to say game workspace dot base plate dot transparency this time instead of brick color now I want to instead of printing it out I want to assign a value to it the way you assign a value when you're coding is use the equal sign then you say whatever number you want it to be I'm gonna give it the number point five so what will happen when we run the script it prints out the messages we had but notice how now the base plate is kind of see-through now note this is the same as if we had gone to the properties window here clicked on transparency change it to 0.5 but the advantage of doing it in a script is that this will happen when your game runs so we can have our base plate completely opaque now but when the game is running something different can happen we can even change it several times this game is really as many times as we want or need to and we can set it to any number we want so it's really powerful and convenient and that's really what we're doing with scripting we're usually for the most part we're just changing a lot of values sometimes we need one thing to be one way or something to be the other way so those are kind of our basic commands we're gonna review two more things in this lesson just to kind of get us prepared for things to come the first one we're going to talk about is a concept called variables no notice we've been doing a lot with the base plate we were printing out the brick color or the transparency but notice every time we've had to print it out or do something to it had to type out this really long phrase and that's I can keep doing that if I wanted to but I'm getting a little tired of it let's say I wanted to change I don't know maybe I wanted to turn off the collisions so that the player falls through the baseplate I really don't particularly feel like typing game that workspace baseplate can collide it's getting a really long kind of want to save myself some work so I'm gonna use something called a variable to make a variable you just give it an you just use this special word local it's a keyword you'll notice how it becomes dark blue and then I'm gonna give this variable a name it can be anything I'm gonna call it my base plate this name can be anything you want for any variable you want in your game you can have as many variables as you want it's a couple things to note you can't have spaces in your variable name so don't do that even though that might be easier to read a couple other things you can't start with like a number there's a few other tricks but those are kind of the common pitfalls just keep those in mind you'll learn what are good variable names as you code in general my recommendation use a short a short word or phrase so you know what this means and then what I'm gonna do is I'm gonna sign again using my equal sign I'm going to sign game network space stop base plate so what's this doing well just like how we assigned the value point five to the transparency of the base plate here right now we're signing game don't workspace that base plate basically this brick right here we're putting that into this variable you can think of it as a little storage device like a little drawer we're just state putting it in there so we can save it for future use and what's really convenient about this is now let's say I want to change can collide of the base plate now all I have to do to do that is just say my base plate dot can collide equals false just as a little tip whenever you see these check marks we want to change them in code those will either be true or false it is checked in that's the same as saying that's the same as making it equal to true if it's unchecked that's the same as making equal to false but back to variables basically this line right here is the exact same thing as typing out this these two do the exact same thing but notice how this one's much shorter and easier makes it so you can write things quicker makes it a lot easier to read really and it's a very nice handy tool to use and just to show that this does something let's click play solo we turn off the closure in the base plate so oh no yeah we fell through and note that variables don't have to necessarily be things in the workspace they don't have to be an object you can make a variable for let's say we want to store a number like let's say I had a score on my game I could start the score off at 0 and I could store that number like that so one more quick thing I want to talk about is the concept of a function now right now we've been you doing a pretty straightforward script all it's been doing is exactly what's been told to do it just goes through one line at a time and does each of these things in sequence pretty straightforward pretty simple now let's say I want to do the same thing over and over again or let's say I wanted to yeah I want to do the same thing over and over again like let's say I had I wanted to set oh I don't know maybe well maybe let's keep going with these collisions so let's say I wanted to keep switching back and forth between can collide and I wanted to change the color so let's say when you can't collide with it my baseplate dot brick color equals brick color red so if you could fall through it it's red well let's say I also wanted to my base plate let's say I wanted to make it so that if you can walk on it let's say we want to make that green so I'm just gonna set the brick color to meet my equal sign right color green and so this is kind of a weird example I don't think your game would actually make the base plate and switch back and forth between can Collider can't collide but I think you can understand this you could probably use this in some other circumstance let's say I wanted to switch back and forth a lot well I could just type let's say any time I wanted to make the base plate you could pass throat and make it red I could just type these two lines again but just like a variable I don't want to waste my time having to write out all these things again what if there is some way I could do it in a quicker way well that's where functions come in so I'm going to make something called a function same way we made a variable we just start with local then we use the special word function I'm gonna call it make base late red and a special thing we have to do is we have to use open and close parenthesis or turn and notice how the how studio will automatically put this keyword end after it now what's special about a function is that all the code that you write that's in between these parentheses in the end that doesn't get run when the program starts as normally as you know programs is going through these one at a time but if it gets to a function it doesn't automatically run the code that's inside it it waits it says okay if I see make baseplate red I'm gonna do the stuff that's in here but I'm until I'm told to do that I'm just going to skip over it for now and so let's say we want to copy this code just gonna cut it actually and put it inside my function notice how actually just to make it simple I'm gonna clear out everything else just to keep it clean so notice how if we run our game base plate is blue think that's because we set the base cake plate color to be blue in the properties yeah but notice how it's not red and we're standing on it that's because this code in the function hasn't hasn't been run yet now to make code in a function run all we have to do is save make base plate red we just use the name of the function and give these parenthesis now it plays solo hey now it's red I pass straight through it notice we've actually been using a couple functions already like for instance print but I was printing hello this is a function it's the it's got a special name that's just the name of the function and it's got parentheses notice here how brick color dot red that's also technically a function then one quick thing you might notice about print is that it's actually got something inside the parenthesis it's not just print like me make baseplate red it's actually got this actually it has something in there and this is what's called a parameter of the function sometimes we don't just want the function to do the same thing over and over again sometimes we want to actually do something specific when we run it so what if we also wanted to print out a message like print we want to say print hey I'm red now but what if we didn't want that message to print out the same thing every time what if we wanted to print out something special well we can give this was called a parameter I'm just gonna call my parameter message instead of printing out this this text I'm gonna print out message now what messages is this basically another variable and when we print it is just going to print out whatever we put into that variable and to put something in we call make baseplate read just so just as we did with print before we can say hey I'm red and notice what happens here we hit play solo yep that's red they printed out hey I'm red because we ran our program it set the variable for the baseplate we made a function it didn't run it right away but it said okay if I see make B's plate red I'm gonna do this code that's between my parentheses in my end statement and I'm actually gonna run the function oh and I'm also have a special instruction to pass along this right here this corresponds to this message and so it printed out am red so those are the very basics we'll be reviewing this a little bit as we go into further lessons because we're gonna be using all these concepts we've just learned and some more we're gonna wrap this up for now but stay tuned in just a moment we are gonna go ahead and get started and make something really cool and interesting with arc we're actually going to have our we're gonna add a game feature so stick around I'll see you in just a couple minutes [Music]
Info
Channel: Roblox
Views: 1,013,402
Rating: undefined out of 5
Keywords: ROBLOX, Lua (Programming Language), Tutorial, introduction, scripting, Studio, Platform Game (Video Game Genre), obby
Id: 0LiaEDui2vE
Channel Id: undefined
Length: 19min 49sec (1189 seconds)
Published: Mon Dec 08 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.