Variables are a core part of all programming languages. They're needed for any complex activity, so, as you might guess, knowing how to use them is important.

To make a variable in Lua (called "declaring" a variable), simply use the formula: variablename = variablevalue. An example is shown on the right. Like other programming languages, you cannot have a variable name with spaces or special characters in it. They also can't start with a number, but can include numbers after the first character.

Unlike some other programming languages, you do not need to tell the program what type of variable the variable you are making is. It is able to figure out whether it's a string, number, or something else. On that note, let's go over variable types!

There's 8 types of variables in Lua, but you'll probably only be using 6 of them for most programs. Those 6 are strings, numbers, booleans, nils, functions and tables.

Starting from the top: Strings. These are bits of text, called strings because they are "strings" of characters. You declare them by putting a set of text in either double or single quotes (" " or ' '). If you want to use either of these characters in a string enclosed by one of them, put a backslash (\) before it. If you want to use a backslash in a string, use a double backslash (\\). An example of a string being declared with these tricks in it is to the right.

Then we have numbers which are, of course, numbers. There are no special syntax things you need to declare a number, besides it having only numbers. An example is to the right.

Next are booleans. As you may have noticed from the index, these will be gone over in detail in another lesson shortly.

Nils are Lua's version of nulls. A nil simply means that there is no data in a variable. Note that this is different from a variable's value being 0 or "", a nil means that there is literally no value there. A funny analogy is toilet paper- an empty roll that just has the cardboard tube is a 0, while having no roll at all is a nil. By default, a variable that hasn't been assigned a value yet is a nil, and a variable that you want to delete can be deleted by assigning its value as nil (although Lua does this to variables that aren't used again automatically). An example is to the right.

Functions are, like booleans, a special case, and will be gone over elsewhere.

Finally, for tables, we will go over those in the next lesson. They are, essentially, a way to store a set of related data, to put it simply.

To get info out of a variable, just use it in place of what you'd normally put. For example, in print(), instead of putting a string in the parenthesis, you put the variable name. Example on the right.

One last thing: If you put "local" before the name of a variable when declaring it, you can make it so that variable is only usable within the context of the function, loop, or whatever other statement it is in. It's alright if you don't know what that means yet, you'll understand later. Just know that, by default, variables in Lua are global, meaning they are accessible from anywhere in the program. It's usually good practice to make all your variables local unless necessary to make them global, to prevent any conflicts or bugs involving variable names.

foo = "bar"

stringExample = "string of text, with a \" quote in it. Also a \\ in it."

numberExample = 1234567810

nilExample = nil

print(foo) --Will print the value of the variable foo
alpha = foo --Sets the value of the variable alpha to the value of the variable foo. Also declares the variable alpha.