On Roblox, scripting can help you make things happen. Without it, nothing would occur. Scripts allow your character to move and blox others, regen things, making things fly (potentially on their own), and much more. However, it takes many precise commands for it to all work. Without precise commands, Roblox may end up doing something you didn't want it to do.
However, the skill of choosing the correct commands and putting them in the correct order is not easily learned. It will take hard work, time, and a lot of practice. If you don't get something the first time, you will need to continue to search for errors, bugs, syntax errors, and continue trying. However, the reward of this is great, for you will be able to script almost anything.
You may wish to visit these links. These are helpful for both beginners and experienced scripters alike, usually as a reference for various commands or classes.
General Scripting Wiki Page
Class Reference Page
Official Lua 5.1 Documentation
Variables allow you to hold a piece of information. This can be one of many things, including a number, string, boolean value, functions, tables (arrays), or any other type of object.
Definitions: A string is a bunch of letters, always put in quotation marks. Ex. "! A string!" is a string, but ! A string! is not.
A boolean value holds either true or false. Objects will be looked at later, but the most basic example is a brick in Roblox. Every brick is an object of the class name "Part".
The name of a variable can be anything other than reserved names. Reserved names are all the names dedicated to commands, and can be found on the official Lua site. They will show up in blue when scripting in Roblox. Note that you generally shouldn't make variable names of standard commands.
Ex. "wait()" is a command, so you shouldn't have a variable named "wait" (although you can)
Variables must start with a letter or an underscore.
When naming a variable, it is best to consider it's purpose. If the variable is supposed to represent the amount of money you have, name it so that it will easily remind you of its purpose. Ex. myMoney, moneyAmount, theMoneyIHave, money, etc.
It is not good practice to use single letters as the names for important variables, since these are usually used in loops (which will be discussed later). Also note that some styles will expect that the first letter of the variable is a small letter and the first letter of the beginning of each word is capital.
Ex: firstLetterOfEachWordIsCapitilizedExceptTheFirst follows the proper style, but is way too long, and doesn't indicate what value it contains.
Variables are used in the following way:
"colour" is our variable, and "game.Workspace.Base.BrickColor" is the value.
There are other things you can do with variables, such as switching their values: a, b = b, a, but it is not essential to know.
Unlike other languages, declaring a variable is not necessary. However, until you assign a value to the variable, its value will be nil. This can be a major source of error if you have a lot of variables and you're not careful in declaring them. However, you can declare a variable local, and must do so if you want it to be private to the function you are declaring the function in. More will be explained on this later.
Properties are variables with special names in an object (such as a brick) and are essential to understand. A property name can be almost anything, just like a variable.
In the above example (colour=game.Workspace.Base.BrickColor), BrickColor is a property of the Base, which is a child of the Workspace, which is a child of the game. Properties Explained in Detail
Note that properties always have a period . while functions have a colon :
Example of a function is object:Destroy()
Example of a property is object.className
Syntax is much like the grammar and spelling of a language (like in English, you cannot say "is sentence this a.", you should say "This is a sentence." In the same way, computer languages such as Lua expect a specific syntax that must be followed.
Help symbols refer to the explaining of syntax. Different symbols indicate different meanings. The main two that are used on this page are [ ], indicating an optional piece of code, and italics, which indicates that code (usually a variable) should be placed there. The next section deals with a preliminary on functions and explains this.
Also note the comment symbol, --, is often used in code to explain why a certain command is being used, or explaining what its purpose is. It can be used as a text block, which can be inserted anywhere and stopped in the middle of a line and other commands can be put after it. Example:
print("Beginning of this line") --[[Normal comments will comment out the entire line]] print("End of line still executes because comment was ended") --The comment block starts with --[[ and ends with ]]. This is useful for commenting out entire parts of a script that you do not wish to delete, but do not wish to execute either.
Lua commands can be put onto a single line and still function. This means that instead of having five commands, each on its own line, they could all be put one right after each other. Naturally, they all need spaces between them so that Lua doesn't think that you're accessing a very large variable name or function name. Example:
a=b*c --note that it does not matter if you put spaces between the operators or not. It could also be: a = b * c.
Would work just as well as:
doSomething() a=b*c print(b+a*c)
Do not worry if the commands do not make sense to you at this point, the basics will be laid out.
Also note that all variables and functions are case-sensitive. This means that "variable" is not the same thing as "varIable".
Since functions make up the easiest commands, it is a good idea to understand what a function actually is. A program (or script) is made up of a series of functions (and in other languages, subroutines) that carry out a purpose. For example, in Roblox itself there will be many subroutines that draw the graphics on your computer screen, and functions that do calculations. In Lua, only functions are used, and have the syntax:
function add(a, b)
print(a+b) --prints the sum of a and b to
The beginning of the function has the word "function", to indicate to Lua that you wish to define a function. The next part is on the same line, and is the name "add". This is not a command, therefore it is fine to use as a function name. The next part is the list of parameters in a pair of brackets (). Each parameter is a variable, and must follow the same rules as before. Note that all parameters are declared local (this will be looked at in chapter 3).
Next there are two commands: "print" and "return". The "return" statement indicates to return the value of the variables after the statement. In this case, that means that this function will return the value of a+b. Then the function ends.
Note what the syntax explanation actually means:
wait() and print() are two very useful, but simple commands, which do exactly as they say. print("Text") will print out information to the Output box, while wait(x) will wait x seconds. Both of these functions are standard commands. These are very useful in debugging. Note that you will often need to use the "tostring(variable)" to convert an object to a string. You may combine strings (concatenate) by using two periods: "123" .. "456" == "123456"
Operators are essential in scripting. Without them, no variables could change, and no calculations could be done. Operators are simple mathematical symbols, including: +, -, /, *, %, and ^. Although you are likely familiar with all of these, here are some examples:
These operators can be applied to all numerical values and printed out in the Output.
Note that you will want to search BEDMAS, the order of operations, if you are confused that the 2nd number to be printed out is 15 instead of 21.
A boolean value can only be true or false. Boolean logic is just a combination of true or false values that produce another true/false value. These are useful later on in loops and conditions.
The main operators that allow you to evaluate boolean logic include: and, or, and not. These are used in every day English, and mean the same thing in Lua. The results are:
true and true == true
true and false == false
false and true == false
false or true == true
true or false == true
true or true == true
false or false == false
The "not" is not as important. It simply negates the value, turning true into false and false into true. This means
not false == true
not true == false
Other operators that are important are:
== means "is equal to" = means "assign the value on the right to the variable(s) on the left" ~= means "is not equal to" < means "is less than" <= means "is less than or equal to" > means "is greater than" >= means "is greater than or equal to" For example, if a=1 and b=2, then using the above operators will give: a==b -->false a~=b -->true a<b -->true a<=b -->true a>b -->false a>=b -->false
Note that you can apply this to boolean logic. For example:
a=1 b=2 c=2 print(a==1 and b==2) -->true print(a==b or a==c) -->false. A does not equal b, and it also does not equal c. print(a==b or b==c) -->true. Although a does not equal b, b does equal c. print(2*a==b) -->true print(not a==b) -->true because a does not equal b. This translates to "not" false, which is true.
Open up Roblox Studio, and make sure that the command bar and output windows are open. Try declaring variables, adding to them, and printing them out. Try these exercises: