Hello, welcome to crazypotato4's complete beginners' scripting guide! This guide is meant for beginners to learn all the basics of ROBLOX Lua. If you are going to read this guide, then make sure that you actually study it, not just skim through it and quit. It isn't impossible to learn to script if you have patience!

In most sections, there will be a link that says "Main Article". This link is just the main wiki page about the subject that the section is about. I suggest reading the version in this tutorial first, then if you really want to, you can check out the main page.


What is Scripting?!?

The first thing you want to know is, what exactly is scripting? Scripting is a type of programming, which is just telling a computer what to do in a language that it understands. In scripting, instead of telling a computer what to do, you are telling an application, such as the ROBLOX game, what to do. Scripting is our way of communicating with the program that runs our games, which gives us endless possibilities.

On ROBLOX, we use a language called Lua to script. It is a very easy language to learn, and commonly used as a scripting language. The version that we use is called ROBLOX Lua, or RBX.Lua for short. RBX.Lua has many things added, which I won't cover until later, and it has a few things removed as well so that people can't use ROBLOX to do bad things to your computer.

Getting Started

Main article: Studio

Now that you know what scripting is, you have to figure out where you're going to script. The ROBLOX script editor is in the ROBLOX Studio application. To open it, go to the Start Menu on your computer, and go to 'All Programs' (maybe just 'Programs' on older versions of Windows, and Mac users will have to figure it out themselves). Scroll down to the ROBLOX folder, and click on the ROBLOX Studio application inside it. The studio browser should open up, and look something like this:

RBX Studio.png

Now, all those buttons may be intimidating to newer users, but it is all easy once you get the hang of it. The first thing you want to do is go to the Menu bar at the very top of the Studio window, and click on the 'View' button. In the drop-down menu, you will want to open up two things. First, you check the "Output":


Then, go to the Toolbars menu and check 'Command'.


Your studio will now have a small bar at the bottom that you can type things into, and a large white panel taking up the bottom third of the game screen.

The two things that you just opened are going to be the tools that we use to start scripting. The output window is there to give us information on how scripts are running. If a script is broken, chances are, an error will show up in the output telling us what is wrong with our scripts, so we can fix them. The command bar is simply a place where we can type in code and press enter, then have the code instantly run, even if the game is paused.

Hello World!

The first script that most people learn when learning a language is a script that will write "Hello, World!" in the output. This has something to do with that being the first thing written on a computer or something, but I really don't care much to explain this. Anyways, back on topic. The way that we tell a script to write something in the output is with the print function. Don't worry, all of this stuff will be explained in due time. For now, all you need to know is that you write the word "print", followed by a set of parenthesis. print() In the parenthesis, we put whatever we want to print into the output. We want to write the words Hello, World! into the output, so we put that in the parenthesis, right? Let's try it. Try typing this into your command bar. print(Hello, World!) Now, if you were to run that, the output will yell confusing things at you with red text. That's how the output shows that there's an error, by telling you stuff in red text. Now why is that? Well, we can't just put words in our script, because the compiler, the code that makes the script run, will try to read it as coding instead of just plain words. To tell it that we want the Hello, World! part to be just words, we put it in quotation marks. print("Hello, World!") When you run that in the command bar, it will successfully output the words "Hello, World!" into your output. Now how is that useful at all? Well, for one thing, it teaches you something, but the practical use of knowing how to write things into the output using the print function lets you find errors in your script that the output doesn't tell you. Again, this will all be explained soon!

Commenting your Code

Main article: Comments_(Scripting)

When you write code, you may want to type things that the script won't read. Maybe you want to leave a note to people on how to edit the script if you are putting a script on Free Models, or maybe you just want to make sure you can look at the script later and still know what it will do (trust me, that will be a problem if you make a long script and want to edit it after a few months or years), or maybe you even want to make part of your code not run, but not have to delete it. Well this is where comments come in. Comments simply tell the script not to look at part of what you type. You make comments by typing two dashes (minus signs):

Code here -- this is a comment, and will not be read by the script More code here

If you want to make a comment go for multiple lines, or maybe you just want to comment out part of a line, you write two dashes followed by two square opening brackets, then end it with two square closing brackets:

Code here --this is a comment, and won't be looked at by the script more code here --[[this is another comment as you can see, this comment goes multiple lines.

D]]more code here

Inserting the Script

Before we go any further, you will need to know how to add a Script into your game. First, while you're in the Studio browser, go into any place in Edit mode, or create a new place:


Now, we're going to insert a Script into our place. On the same menu bar that we found the View dropdown menu on, we find one that says Insert. On this menu, we want to click the Object button, and a new window will pop up.


Here, you want to find Script, and press the Ok button. In your Explorer, on the right side of the window, you should see a Script appear in the Workspace directory. Double-click the script, and it will open to a white page with a Hello World script on it. Delete that.

To close the script, go back to the Menu, and click the File menu and hit 'Close':


And now you are ready to move on to the first lesson.

Lesson 1: Data and variables

You may be thinking right now, "O NOES! DERE ARE CONFUUZIN WURDS IN DA PAGE, SCRIPTING ISH 2 HARD 4 MEH!!!1!!11!!one!". Or you might not be. Either way, don't worry, this is all very easy to understand, just read this part carefully.


Main article: Boolean

The first data type that I will explain is the boolean type. Yes, the name is silly, and you probably haven't heard it before. Well, booleans are very simple, as they are just the values true and false. It's really as simple as that, true and false, yes and no, on and off. That's it. See, simple!


Numbers are a more complex data type, but you have probably heard of numbers before. If you haven't, I suggest paying a lot more attention in school. There are actually two types of numbers in programming, although in Lua, only one of them is really used.


Main article: Integer

Integers are the more basic type of number. They are just all <whole> numbers, both positive and negative. In programming, there is a limit to how high a number can go, because only a certain amount of data is given to each number, so it can only take up that much data. It's as if the numbers (and all other data) are water and they are held in certain sizes of bottles, they can't have too much liquid or it will overflow. In programming, the integer type goes to a maximum of 2147483647 (2^31 - 1) and can go all the way down to -2147483648 (-2^31). The reason that we have those exact numbers is because integers are stored in 32 bits (binary digits) of memory. One is used to tell whether the number is positive or negative, so that leaves us with 31 bits, which can make 2^31 different numbers. You don't have to know all of this, don't worry, it's just for anyone who is wondering where these numbers are coming from. You probably won't have to worry about these limits, as they are over 2 billion, so you most likely won't need that big of a number any time soon. Plus, the scripting language that we use, Lua, doesn't use integers, it uses the float type, which we will discuss next.


Main article: Number

What exactly is a float? Why is it categorized under "Number"? We aren't talking about the big balloon cars at parades or anything, we are talking about numbers. Float is short for floating point single precision number. Don't worry, it's not complicated. Let's break it down.

floating point - That means that the decimal point can move around, so you can have your number be 2.4, then move the decimal to the left and change the number to something like 1.7309.

single precision - That is just saying that the number has single precision, so it loses its accuracy around six decimal places.

number - This should be obvious. If it needs explanation, then go ask your mom or your math teacher or someone. You can even do a Google search and get all sorts of stuff about programming and math folklore.

In Lua, all numbers are actually doubles, which are just like floats, but more accurate. There is no other number type in Lua. Yes, we just talked about an integer type, but that is just in general programming. Lua doesn't use integers.


Main article: String

This should be the first thing in this tutorial to actually make your brain work to understand it. The rest of it has been child's play.

Strings are just a bunch of characters strung together. But what are characters? They aren't the little people in your video games, they're just single letters, numbers, and anything else you can type on your keyboard. Even spaces. Even things that you can't type in on your keyboard. Everything is a character.

To create a string, you can do one of four things.

1. Surround stuff with quotation marks (remember back in the Hello World part of this tutorial? That was a string!) Ex. "This is a string" 2. Surround stuff with apostrophes (single quote marks) Ex. 'This is also a string' 3. Surround stuff with two brackets (the sqaure thingys). These strings can take up multiple lines. Ex. [[This is a string. This is still a string :D

ya im still a string. ujelly?]]
4. Surround stuff with one bracket, then any amount of equals signs, then another bracket. The amount of equals signs on both sides of the string must match. This type of string can also contain multiple lines. Ex. [===[This is an epic string

D]===] This works the same way with comments, and can be helpful for when you want to be able to use different strings inside comments and not have them interfering with each other.

The first method of making strings is the preferred one, since it colors the text pink when you type it in an actual script. One reason that there are so many types of strings, though, is so that you can stack the symbols inside each other. For example: "hi this is a string "this is supposed to be a quote mark inside the string, but instead it just ends the string, so now this is treated like real code"this is another string" Instead of breaking your script by doing that, you can just use different kinds of strings. this is a string "this is still in the first string, it didn't start a new string or end the first one :D" and this is all still a string


Main article: Nil

Nil. That's a funny word, like Boolean.  :D

Now back to being serious. The nil data type represents the absence of a value, or basically nothing. Nil is just for when there isn't anything in a value.


print(NonexistantVariable) -- prints 'nil'

Var = nil -- omg colorful :D

print(Var) -- prints 'nil'


Main article: Variables

Now, you have all these different types of data, but where are you going to put them? We need something to hold the data in so we can keep track of it until later, otherwise it will just get lost in oblivion. Well that's why we have variables. Variables are a way of naming your data so that you can find the data whenever you want in your code. They can also be used to make your code a lot shorter, so you can just type a piece of data once and then just be able to type in a little word and all that data is right back at your fingertips.

Now like I said, a variable is a name for your data, so you are going to make the name of a variable equal the data. To do that, we just use one equals sign. On the left of the equals sign you put the name of the variable, and on the right, you put the data. The data can be any of the types of data mentioned above, and more that I haven't talked about yet.

Variable = 7

Now the variable "Variable" holds the data 7. Now, in our script, whenever we type Variable, we mean 7. A simple example of how you can use this to shorten data is with the number "pi". it is a number with millions of decimal places discovered, and still more being found. Obviously, our numbers can't hold millions of decimal points, but it can hold enough that it would be annoying to have to type it over and over again. We can use a variable to solve this problem!

pi = 3.14159265359

There, now instead of having to type that long number out, we can just type the short word "pi". Yum.

And then also with strings:

str = "this is a really long string that would be annoying to type out every time you needed to access it instead of just being able to type 'str'."

This works with any type of data, or even other variables (remember, variables are just another name for your data).


Indentifiers are the names for your variables. When you say, for example, Var = 7, 'Var' is your identifier.

A valid Lua identifier is made up of letters, numbers, and underscores. They also must start with a letter or underscore.

Examples of valid identifiers:

  • _Hi
  • Potato
  • number42
  • trololo
  • my_name
  • the_number_6

Examples of invalid identifiers:

  • 6_puppehs
  • q~q
  • my-name
  • hi mom

Lesson 2: Tables

Main article: Tables

Now, you have these simple data types, and you have variables to hold that data, but now, what if I want to store multiple pieces of data and only have to use one variable to do it? Well, obviously, since we are talking about this in a section about tables, those are what can do this. Tables are a type of Object in Lua, which just means that it is passed by reference instead of by value. We'll get to that later, first we want to learn the basics of tables.

Tables are defined by a pair of braces.

Table = { }

If you can't see clearly, they're the curly things you get by pressing Shift + square bracket (first and second keys to the right of 'P' on a QWERTY keyboard).

The table holds what are known as key-value pairs. The value is any type of data (string, number, boolean, even tables!), and the key is usually either a number or a string. If you don't manually assign a key, then the keys are numbers representing the position of the value in the table (first value has a key of 1 unless you say otherwise, etc.) To list values in the table, you simply add values separated by commas or semicolons.

Table = { "Value", "Another value", true, 1.7, { "This is a value inside a table inside a table :D" } }

The last value in that table is another table, which contains 1 value. To access one of the values, also known as indexing the table, you use a set of square brackets with the key inside them. Like I said, the first value has a key of 1, the second has a key of 2, etc.

print( Table[4] )

That would print '1.7' to your output (assuming you have 'Table' defined as I did above).

Now, you may be wondering why we didn't put quotation marks around the stuff inside the print. I said to do that earlier, right? Well, as you have now learned, quotation marks show that you are making a string, so none of the stuff you write in the string is actually read by the script. We want the stuff inside the print to be read by the script, so we don't make it a literal string (literal strings are just strings that are defined explicitly: you aren't using a variable to refer to the string, you're actually putting the string there).

So now, you might be wondering why the print function didn't error when we tried to put a number into it. Well, print simply uses a function that turns anything into a string before it tries to put it into the output. It's quite simple, and we will get to that later. I promise.

Now, I mentioned keys earlier, and how they can be both numbers and strings. Well how might you define a key as a string? How can you define a string at all, it seems that the script does it for you!

To put a key, it's just like assigning a variable to a value. You just put the name of the variable (or the key), then an equals sign, then the value that you want to assign it to.

Table = { Potato = 4, Tomato = "hi" } print(Table["Tomato"])

Output: hi

If you still are fuzzy on the concept of key-value pairs, here's a better way of looking at the table:

Table = { Potato = 4, Tomato = "hi" }

As you can see from that, Potato and Tomato are the keys, organized in one row, and 4 and "hi" are the values, organized in the other row.

You can index string keys both by using the square brackets, or by simply adding a dot between the table name and the name of the key.


Output: 4

If you use a table inside the table (called a multidimensional table), you can do this multiple times, since indexing the table inside the table will give you another table to index.

Something = { OtherTable = { Something = { "Cool" } } } print( Something.OtherTable["Something"][1] )

Output: Cool

As you can see, you can use both indexing methods at the same time. Or not. It's up to you.

TIP If you want to explicitly set anything other than a valid Lua identifier as an index in a table, you must define it by surrounding it in square brackets (ie [Key] = Value), and index it using square brackets as well (Table[Key], not Table.Key).

Reference vs. Value

Now, earlier, I told you that tables were a type of object, which means that they get passed by reference. Here, I'll explain that:

There are two ways for values to be passed: by reference and by value. When a value is passed by reference, that means that a variable referring to that value is really looking at where the data for that value is stored in the computer. When it is passed by value, a variable referring to the value will simply take a copy of the value. In Lua, all of the simple data types that we talked about earlier (strings, booleans, and numbers) are passed by value, and objects, which are more complex data types, are passed by reference. Here is a short example that shows this in action:

Var1 = 4 -- this is a number, which is passed by value Var2 = Var1 -- making Var2 equal to the value that Var1 holds. Since it's a number, and is passed by value, Var2 is now just a copy of Var1, so Var2 = 4 Var1 = 7 -- now we change Var1 to 7, but Var2 will not change since the number was passed by value rather than reference print(Var1) print(Var2)

Output: 7 4

Tab1 = { "hi" } -- Tab1 is a table with its first index (Tab[1]) equal to "hi". Tab2 = Tab1 -- now, Tab2 refers to the table that Tab1 contains Tab1[1] = 4 -- change the first index in Tab1 from "hi" to 4 print(Tab1[1]) print(Tab2[1])

Output: 4 4

As you can see, the value in both tables changed because the table was passed by reference, so Tab1 and Tab2 both are the same table, not just one table and a clone of that table.

Lesson 3: Operators

Main article: Operators

Operators are quite simple. Operators are symbols or keywords that modify values. The values that they change are called operands. There are two kinds of operators, unary and binary. These are kind of fancy words, but they're very simple.

Unary - Un = One, so unary operators change one value/operand.
Binary - Bi = Two, so binary operators change two values/operands.

All of the operators in this section are binary operators unless it says otherwise.

Arithmetic Operators

The first type of operators are arithmetic operators. Arithmetic is just a fancy word for basic math (everything before Algebra/Pre-Algebra), if you didn't already know. The operators here are all quite simple, and you should already know most of them.

  • The '+' operator adds two numbers. 1+1 is 2.
  • The binary '-' operator subtracts two numbers. 5-4 is 1.
  • The unary '-' operator changes the sign of a number from positive to negative and back. -(5) is -5.
  • The '*' operator multiplies two numbers. 4*2 is 8.
  • The '/' operator divides two number. 6/3 is 2.
  • The '^' operator raises one number to the power of another number. 3^2 (32, 3 squared) is 9.
  • The '%' operator, also called the modulo or modulus, gives you the remainder of a division. 5%3 is 1, because 5/3 is 2 with a remainder of 1.

Comparison Operators

Comparison operators do just as they say, they compare two values. All of them will give you booleans, which we learned about in lesson 1.

  • The '==' operator (two equals signs) checks if two values/operands are equal to each other. This is known as the equality operator.
  • The '~=' operator ("~" is shift and the key to the left of the '1' key) checks if two values are not equal to each other. This is often *called the inequality operator (not to be confused with mathematical inequalities).
  • The '>' operator checks if the value/operand on the left is greater than the one on the right. This is known as the greater than operator.
  • The '<' operator checks if the value/operand on the left is less than the one on the right. This is known as the less than operator.
  • The '>=' operator checks if the value/operand on the left is greater than or equal to the one on the right. This is the greater than or equal to operator. Just like the one in math class (≥)
  • The '<=' operator checks if the value/operand on the left is lessn than or equal to the one on the right. This is the less than or equal to operator. Again, just like from math class (≤).


print( 5 == 5 ) --> true print("wat" == "wat" ) --> true print( { "hi" } == { "hi" } ) --> false (this is because of that reference vs. value stuff; these are two completely different tables) print( 2 + 1 > 3 ) --> false print( 7 <= 7 ) --> true

Logical Operators

When using these values, nil is treated like false and all other values are treated as true.

  • The unary 'not' operator negates a boolean value. What that means is that it takes the opposite of a value. not true is false, not false is true.
  • The 'and' operator basically checks if both values are not false, or both values are true. This is the simple way to look at it. true and true is true, false and true is false, false and false is false.
  • The 'or' operator basically checks if at least one value is true. true and false is true, true and true is true, false and false is false.

PROTIP The real way that the 'and' and 'or' operators work: and checks if the first value is true, if it is, then it gives you the second value, if it's not, it gives you the first one. or checks if the first value is true, if it is, it gives you the first value, if it isn't, it gives you the second. If you don't understand this stuff, just stick with the simple definitions.


print( 4 or true ) --> 4 (the first value is considered true, so it gives you the first value print( true and "hi" ) --> hi print( nil and false) --> nil print( nil or false) --> false

Other Operators

  • The '..' operator is known as the concatenation operator (you don't have to remember it if you don't want to), and it combines two strings. Ex.print( "hi" .. "mom" ) --> himom
  • The unary '#' operator gives you the length of a string or a table. For strings, this means the number of characters. For tables, this means the highest numeric index. Ex.

print( #"hello" ) --> 5 print( #{ "potato", false, { "q", "lol" }, 42 } ) --> 4

  • The '=' operator assigns a variable or index/key to a value. It is called the assignment operator. Examples of this are in the sections about variables and tables.

Page 2

Go to this link for page 2 of this tutorial, because I didn't want to cram everything into one page: