Rambling Scripting Tutorial
Lua is the name of the language, and it's very easy to learn. It's designed that way.
In Lua, there's values and variables. Values are data. Examples are "Hello", 5, true, false, nil (that means nothing). Variables hold values (which are data).
Lua is designed to be embedded into host applications to expand on functionality and general awesomeness. ROBLOX has Lua 5.1.4 (that's a version number) embedded into it.
ROBLOX games are made of objects which hold values. It's organized in a tree-branch-leaf type of system. In code, it uses the analogy of parents and children. The parent object that holds all the other objects as descendants is called "game". In code, it's referred to as "game".
Wait, what's an object? An object (also known as an Instance) has properties with values. They also have methods, or functions (functions themselves are a type of value). These methods and properties are part of ROBLOX's core.
What are some examples of objects? Anything you can and can't see inside a game - bricks, force fields, leaderboard stats, GUI objects - pretty much everything.
Everything in Lua is identified in some way. There's two kinds of identification: reference, and literal. To identify something by reference, you have an identifier which can only contain upper and lowercase letters, numbers, and the underscore (_). Also, they can't begin with numbers (because then they might be a number literal, which you'll read later). By the way, if you want to refer to a Lua identifier in normal English, use the back tick marks: ``. It's next to the 1 key on American keyboards.
By custom, you should not begin your variables with an underscore. You can do so, if you want to make puppies cry.
Remember `game` from earlier? That's an identifier that points to the Grand Poobah of all objects in an ROBLOX game. Note that for later.
When you want to identify a value in Lua code, you use a literal. Literals are specific values represented in code itself. You write these in your code. It's a complex way of saying describing something really simple. You'll learn about examples of literals next.
Let's go back to values. Values are like elements. They have types that can't be broken down into simpler terms. Each type has a specific kind of use in programming. The value types are (you should write these down):
These are the bare bones of Lua. Let's go into detail on them all:
Strings
A string is a combination of characters (letters, numbers, symbols, everything). They hold text and words. They are represented in code by using quotes: "hello". You can use single quotes if you want to as well: 'hello'. There's different, more advanced reasons for using either though. Examples of string literals:
"Hello world" 'Ozzy is a fun person' "150,525.5"
Numbers
You know what a number is - you count with them. Numbers in Lua are represented by any number of the following characters: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, then optionally a period (decimal point), then any number of those previous numbers. Examples of numbers:
3 17 9001 3.14 0.5782 0.001
Boolean
Another fancy name for a simple concept. Booleans can only be one of two values: true or false. It's like a light switch: on or off. Up or down. In or out. High or low. You get it. Booleans are only represented by these two keywords:
true false
Nil
It means a state of emptiness or non-being. If a variable has a value of nil, it holds no value. It's also said that the variable is nil. Nil values are only represented by the "nil" keyword. Setting a variable to nil is essentially "deleting" the variable.
nil
Function
Functions represent doing something. It's like a verb. Only these kinds of verbs aren't like English verbs. They work different. Functions work with variables and values. They contain Lua code. They can be provided values to work differently. They bring Lua to life. An example of a use of a function is to give you a random number. To kill a player. Or to shoot a laser, in which you have to tell where to shoot it. You might recall earlier that objects in ROBLOX contain methods, or functions. Methods are functions that an object has. We'll discuss function literals later when working code is taught (because that's what function literals contain - more code).
Table
Tables are fantastic! They are what makes Lua fun. Tables store values in a list-type fashion. Tables have keys and values. Each key points to a value. Keys can be any value type (except nil), but most of the time the keys are numbers or strings. If you had a table that used numbers as keys, you'd have something like a shopping list: 1) "Apples" 2) "Milk" 3) "Coffee" However, that's now how you write a table literal in Lua code. Writing table literals more complex, so we'll put that one off for now. Just understand the concept of tables as keys pointing to values packaged into one nice value type.
Userdata
Userdata is the easiest concept to know. Remember how Lua is embedded into host applications? Well, the host application can make its own data values for use in Lua. Why would you want to do that? It's because usually the host application is faster than Lua as a language. It also could be that there's another basic concept of a value that should be considered as such, but isn't in normal Lua. Examples of userdata include 3D positions, 3D orientations, Colors, etc. You might imagine why ROBLOX would want some of those! Userdata work like tables, but you can do more with them if they are designed like that. For example, you can add 3D positions together in ROBLOX using userdata called Vector3. Userdata are not definable in Lua code a literal. Since the work like tables, Lua can build userdata by using tables. However, since Lua isn't the host application, it can't do as much as the host application.
That's all the conceptual stuff you need to know about Lua. And guess what - most Lua programmers don't even know that much. You haven't written one line of code, yet you're already further on in some places than other people. Yay.
Let's go on to actually doing things in code that have already been stated. Since we've already gone over the conceptual stuff, this stuff is REALLY easy.
To set a variable (aka, set it's value), first state the identifier, an equals sign, then the value it should be set to (or an expression that comes to one value, like 3 + 5 resolves to 8). The value can be either a variable which identifies another value or a literal.
score = 5 name = "Ozzypig" grand_poobah = game flag_up = true
Wait a second, I haven't told you how to not do something in code yet. These are known as comments, and comments are text in code that is not processed as code, but instead ignored completely. As long as you write them correctly, they will be ignored completely.
The first kind is a simple single-line comment. These start with two dashes:
--Hi, I'm a comment --I'm so alone - I'm completely ignored! --But you're here to use me to document code!
The second kind is a multi-line comment. These go for multiple lines. They start with two dashes again, but use double square left brackets to continue until two right double square brackets:
--[[ I'm a multi-lined comment. Why didn't you use me before? I would have been so much more convenient! ]]
Single line comments can go on the same lines as code, but they have to go after the code (otherwise the code is ignored as a comment):
red_team = 0 --this resets the red team's score
Input and Output
Wait, let's talk about input and output. What is input and output? Input and output is data that goes in and out of a program. Input is data you give the program to work with (however some programs might not even need input). Output is the results of a program. Output could be anywhere from the cool effects you see in a game to just an answer to a math problem.
How does Lua give/get input and output? Well, remember that Lua is embedded. It varies between the programs that use Lua. Let's talk about ROBLOX though: Input and output in ROBLOX Lua is different. Output could be a message on the users screen. However, we'll keep things simple by using simple output and no input yet (all the input will be coded right into the programs as literals).
Remember functions? Lua has a lot of built-in ones that ROBLOX has re-defined so that they can work with ROBLOX games and ROBLOX studio. The simplest form of output is textual - most values can be put into text form (especially strings, haha). The Output window in ROBLOX studio displays values given to a function called "print".
To call a function (like print) you state the identifier to it's name, an open parenthesis, the values that will be given to the function, then a close parenthesis:
print(5) --This shows "5" in the output on one line print("Hello world") --This shows "Hello, world" on the next line.
When talking about functions in English words, you use the name of the function and then parenthesis after. If I want to say how cool that print function is, I say "print() is awesome!"
The print() function helps you keep track what your program is doing. Often programmers write something wrong and have to inspect the values of variables so that they are what they should be. For now, we will just use print() to inspect the value of variables as they are change by code.
Let's do some math now that we know how to figure out what our variables' values are.
a = 5 b = 3 print(a) --5 is displayed print(b) --3 is displayed
Math and other simple operations are performed by operators. Operators can be unary, binary, or ternary. Woa, what does that mean? Simple: it means that they need input in the form of one, two, or three values (respectively). Addition is done by using a plus sign (+):
c = a + b print(c) --> 8 (Lua added a, which was 5, and b, which was 3 to make 8).
Subtraction is done by using a negative (-) (dash) sign:
c = c - 1 print(c) --> 7 (subtracted 1 from c, which is 8, to get 7).
Multiplication is done by using an asterisk or star (*):
c = c * 4 print(c) --> 28 (multiplied c, which was 7, by 4 to get 28)
Division is done by using a forward slash (/):
c = c / 7 print(c) --> 4 (divided c, which was 28, by 7 to get 4).
Note how the division operator looks like a fraction!
If you're in a decent math class, you should know the Order of Operations, or PEDMAS. This applies to Lua as well. It solves parenthesis, exponents (which you haven't learn exponents yet), division/multiplication, addition/subtraction. Consider this:
print(1 + 2 * 3) --> 7
It multiplies 2 and 3 to get 6, then adds one to get 7. If you thought that it added 1 and 2 to get three then multiply by 9, you violated the Order of Operations in Lua, and you should slap yourself on the wrist with a wet noodle.
By the way, in programming, Order of Operations is called precedence. Somethings precede others (A comes before B). Order of Operations is a term used in the "math world", precedence is used in the "programming world", including Lua.