Scripting Lesson 001: Intro to Scripting

Beginning

Intro to Scripting

Hi, my name is Domswolf, and welcome to our very first scripting lesson together. Not only me, but many other developers can help you through written, live, or personal lessons through this course. Before we get started on our first exercise I want to explain a few things you need to know.

Scripting can be a very difficult thing to grasp, even I've been scripting for only about a month now, but that hasn't stopped me before. Feel free to move and practice scripting at your own pace or ask questions on roblox.com by PM-ing me, or another developer. Scripting just takes some understanding and practice to master.

Now before we start explaining things, I have mentioned in one of my building lessons that free models were something you don't want to use. The truth is, free models can actually be a very good way to learn and understand Roblox Lua, however you should always try to use your own scripts in an actual project.

Ok, so we're going to start our by learning some basics. First let's place a brick into our Studio.

Now go to your "View" tab and click on Advanced Objects. There you will see a script. Click on your brick and double click on the script to insert it into your brick.

Now you have two things: A child, and a parent. A parent is whatever the script is inside, so commonly in scripts you will see "script.Parent" (identifying the brick it is inside). The child is obviously anything that is inside of another brick. A child can be a mesh inside of a brick for example. Even bricks are children of the Workspace, and the Workspace is a parent of game. Game has no parent.

So what we are going to do here is make a script that, when touched by a player, changes any factor or property of the brick. In the script we want to add what is called a function. There are many different functions to use but the one we will use here goes like this:

This means that the script will activate and run its course when a person touches the brick. Now that we have that in there, press enter and start a new line, you will see an "end" at line three. Putting an end is (obviously) what ends the script. In line two, we want to change the transparency of the brick. (If you want to work on a more gradual transparency change then I will talk about that later). Since we are changing the brick that the script is inside of, we type script.Parent to identify the brick we want to change.

Now whenever we continue with something that isn't parent, it will usually indicate that whatever we are changing is inside of the brick. (For example, script.Parent goes up through the "geneology" of the game, and otherwise it goes down.) Transparency is a property of the brick so we would type script.Parent.Transparency = 0.5 (if we want to set it to 0.5 when the brick is touched). We can do the same with the name of the brick (script.Parent.Name) and tons of other things. Properties like Position, Rotation, and Color will be discussed in other lessons.

You may notice that your script doesn't work. In fact you won't. The reason why is that you need to link up your function to this final line. After line 3, click enter and type in this (connects to function):

Want to keep going? No? Well if you are not brain-hurt yet, keep on reading (extra practices on the bottom)

Now what if we want to change it back to 0 in game (Transparency). To do this we can do two things, have the script switch back and forth between 0 and 0.5 when touched, or make another brick that changes it to 0, which is what we're gonna do because it's another skill we need to have.

So insert another brick next to your other one with a script inside of it just like the last one. The script will also have function onTouch(hit), but instead of script.Parent, we wanna change another brick. Remember how I told you that all bricks are in game.Workspace? (unless in a Model or course) Well here is the reason why. What we are going to do is name the new brick Part2 (while the other stays Part). This way we don't have any errors or risk editing both bricks at the activation of a script. We will type in line 2, "game.Workspace.Part1" before doing anything regarding Transparency. We are left with this:

Great job, you've covered a very basic element of script understanding and can now explore possibilities on your own. Though they may be limited, there are plenty more lessons to come.

Extra Practice- Make a script that gradually changes the transparency of a brick over time (short time)

Challenge- Make a script for an automatic opening door (can either slide or change transparency)

-Domswolf