Intro to Scripting

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:

print("Hello world!")

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)

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 "".

It is important to spell print exactly as you see above with all lower case letters. Programs are very specific on how commands are spelled and capitalized. If you spell or mis-capitalize a command, you will see an error in the Output window like this: Hello world script error.png


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)

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)

Variable Names

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:

  • Variable names cannot have spaces in them.
  • Variable names cannot start with numbers.
  • Variable names cannot have any special character except for underscore: _.
  • Some words in Lua are reserved for special uses. Variable names cannot be the same as these special words. A few examples are for, and, end, if, and while.
  • Variable names should not be the same as functions. For example, you technically can use the word print as a variable name, but it would ruin your program as that would overwrite the actual print function.

Basic Math

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)

We print all kinds of operations:

> print(10 + 5)
> print(10 - 5)
> print(10 * 5)
> print(10 / 5)

We can also perform math on numbers stored in variables.

> myNumber = 5
> print(myNumber + 2)

We can even store the result of operations into a variable:

> sum = 25 + 8
> print(sum)

In the next tutorial we will cover how to change the properties of parts through code.