How to Script Anything/Chapter1

Contents

[edit] Chapter 1: Scripting Basics

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.

[edit] Helpful Links

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

[edit] Variables

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:
variableName=value
Ex. colour=game.Workspace.Base.BrickColor
"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.

[edit] Properties

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

[edit] Syntax and Help Symbols

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:
doSomething()
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.
print(b+a*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".

[edit] Functions, Parameters, and Return Values

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 name([param1[,param2[,...]]])
[Commands here]
[return [value]]
end

Example:
function add(a, b)
print(a+b) --prints the sum of a and b to
return a+b
end
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:

  • The first word is "function"
  • You must provide a name after the word "function"
  • You may have parameters (as many as you want), but you do not need them.
  • You may put commands in your function, but do not have to.
  • You may return a value, but do not have to. You may wish to return without providing a value to simply exit the function. "nil" is returned by default.
  • You must end the function with the "end" statement.

[edit] wait() and print()

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"

[edit] Operators and Boolean Logic

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:

  • 4 + 2 = 6 --addition
  • 4 - 2 = 2 --subtraction
  • 4 * 2 = 8 --multiplication
  • 4 / 2 = 2 --division
  • 4 ^ 2 = 16 --4 to the exponent 2 (which is the same as 4*4), or 4 squared.
  • 4 ^ 0.5 = 2 --the square root of 4
  • 4 % 2 = 0 --the remainder of 4 divided by 2 is 0. This is the modulus operator.
  • 4 % 3 = 1 --the remainder of 4 divided by 3 is 1


These operators can be applied to all numerical values and printed out in the Output.
Example:
number=3
print(number)
number=number+4*3
print(number)
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.


[edit] Try it out!

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:

  • Make a variable and assign it a value of 10
  • Make another variable and multiply it to the first, printing out the results
  • Try printing out some comparison operators, with or without boolean logic.
  • Question for practice: What is the result of " 1+1*2 == 3 and 3*3==9 " ?


Back to How to Script Anything