Intro to Scripting

(Redirected from Your first script)

This is an easy tutorial This is an easy tutorial. Creating Parts via Code > This tutorial is part of a series on scripting. Return to tutorial index

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.

CommandBarButton.png

OutputButton.png

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.

Print[edit]

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(7)
7

Notice how we did not put 7 inside of quotation marks "". If we are printing out a number and not a string, we do not need to include the "".

Basic Math[edit]

We can also print out the result of 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

Variables[edit]

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

Like print, variables also work with mathematical operations and can store the results for future use:

> theSum = 5 + 6
> print(theSum)
11

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:

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

In the next tutorial we will cover creating parts and changing properties through code.

This is an easy tutorial This is an easy tutorial. Creating Parts via Code > This tutorial is part of a series on scripting. Return to tutorial index