A script is a series of instructions for a program to follow to create custom behavior. In order to give these instructions, you must provide the instruction in words the program can understand. Roblox uses a language called Lua to communicate these instructions. In this set of tutorials, we will go over the basic instructions you can use in Roblox Studio to create games.
To get started, we need to open the Output and Command Bar panels. To do this, click on the View tab and then on both the Output and Command Bar buttons.
The Command Bar shows up at the bottom of Studio by default, and is a place where we can write instructions for the game. The Output window will show any kind of text responses from the game and will let us know if we make any mistakes.
The first instruction, or function, we will cover is called print, which makes text appear in the Output window. In the Command Bar, type:
After you are done typing, hit enter. The Output panel will show both the instruction and the message we wanted to print:
> print("Hello world!") Hello world!
print will output whatever is in between the parenthesis () that follow it. In this case, we printed out the sentence: Hello world!. When you want to print out sentences, which in Lua are called strings, you have to surround the sentence with quotations.
We can print other things, like numbers:
> print(10) 10
Notice how we did not put 10 inside of quotation marks "". If we are printing out a number and not a string, we do not need to include the "".
In scripting we often want to store information to be used later. We can do this by using variables. You can think of a variable as a box you put information in. To create a variable, all we need to do is give it a name and assign a value to it. For example:
> myFavoriteNumber = 7
The above line of code stores the number 7 into a variable called
myFavoriteNumber. First, we are saying the name of the variable we want to use, in this case myFavoriteNumber. Then the equal sign says we want to store something into that variable. After the equal sign we put the value we want to store.
Notice how nothing is added in the output after we enter the above line. That is because the print function was not used. If we want to see what is stored inside of myFavoriteNumber we can put it inside a print function like this:
> print(myFavoriteNumber) 7
We can also store strings inside of variables. Just like when we printed strings before, the string has to be surrounded by quotation marks "".
> myFavoriteAnimal = "Dog" > print(myFavoriteAnimal) Dog
We can give variables almost any name we want. There are a few rules one has to follow in Lua though when naming variables though:
We will often need to do math while scripting. Lua supports all the basic mathematical operations. For example, if we want to print the result of adding two numbers, all we have to do is:
> print(7 + 3) 10
We print all kinds of operations:
> print(10 + 5) 15 > print(10 - 5) 5 > print(10 * 5) 50 > print(10 / 5) 2
We can also perform math on numbers stored in variables.
> myNumber = 5 > print(myNumber + 2) 7
We can even store the result of operations into a variable:
> sum = 25 + 8 > print(sum) 33
In the next tutorial we will cover how to change the properties of parts through code.