User:JulienDethurens/Guide/Chapter 1

From Legacy Roblox Wiki
Revision as of 23:38, 22 January 2012 by >JulienDethurens (→‎Strings: The fail syntax highlighting of the wiki is still 2x better than the studio's syntax highlighting, it seems. It can recognize escape characters, while the studio's script editor can't even highlight multiline strings...)
Jump to navigationJump to search

Note: considering we are not yet in the ROBLOX-specific section, you can use the Lua demo, on the official Lua website. You can use it to run code. However, note that it uses Lua 5.2 and that ROBLOX uses Lua 5.1. Therefore, some of the things I will teach you will not work with it. Don't worry, though, almost everything still works in Lua 5.2. And I'll try to tell you whenever something I am talking about is no longer available in Lua 5.2.

I suggest using the Lua demo to test code, as it avoids you the trouble of starting the studio and everything. But you can also use the studio if you want.

Syntax

First of all, what is syntax? What does that word mean? Well, syntax is actually like grammar, but for a programming language. It explains how you must structure your scripts.

ROBLOX Lua uses exactly the same syntax as Lua. Only functions were edited and added, as well as removed, but the syntax is exactly the same. You can find the syntax of Lua in extended BNF here, except I promise you won't understand anything: The Complete Syntax of Lua.

Don't worry, though, you're not going to have to understand any of that. It's extended BNF, which doesn't really have anything to do with what you'll need to learn.

Names

Names, in Lua, are also called identifiers. They can be any text composed of letters, digits, and underscores and not beginning with a digit. They are used to name variables and table fields (you'll know what both of these are later).

Here are some valid names:

name
hello
_
_tomatoes
me41
_thisIs_StillaValid23name

Here are some invalid names:

2hello
th$i
hel!o
563text
82_something

Also, the following keywords are reserved by Lua and can not be used as names (oh, and you'll notice that they're all colored in yellow, they will be colored the exact same way in all of this guide):

and break do else elseif end false for function if in local nil not or repeat return then true until while

When naming a variable or a table field, you must choose a valid name for it. It must therefore start with a letter or an underscore and only contain letters, underscores and digits.

You don't know yet what a variable and a table field are, but don't worry, I'll explain that soon. However, first, I'll talk about comments, then, about the different types of data.

Note: Lua is case sensitive. This means that 'Hello' and 'hello' are two different names.

Comments

I'm talking about these right now because I'll use them soon, so I want you to know what they are.

A comment is simply text that is ignored by Lua. You can use comments to describe one or many lines of code, or for any reason you want to.

A comment starts with a double hyphen, like this:

-- This is a comment!

Usually, I tend to put a space between the hyphen and the text, but it's just a preference. You can do this too:

--This is a comment!

And you can put the comment on the end of a line of code too:

print 'Hello world!' -- This is a comment that was put after a line of code!

These are simple comments. They only continue until the next line. However, sometimes, you might want to make a comment that continues on many lines. You can do this with a long comment.

A long comment starts with a long bracket. Here is an example:

--[[ This is a comment This is still in the comment This is in the comment too! ]]

Sometimes, you might want to use comments to disable code temporarily (because, remember, Lua ignores everything that is a comment and doesn't run them). In a such case, it is possible that you run in the following problem:

--[[ variable = this is a string, and the other thing is a variable, but you don't need to know about that yet ]]

Don't worry about the code, it is just an example. The point is that double brackets can also be used for other things than long comments. But there is a simple solution:

--[===[ This is another long comment and you can still put text on any line. ]===]

As you can see, the comment is not colored here. That's because I disabled the syntax highlighting on this one, because the syntax highlighting doesn't understand this type of long comment. Trust me, though, they work.

I will use comments a lot in this guide, so don't be surprised if you see one.

Types

You must know that there are simple data types, and more complex data types. Here, I am only going to talk about data types you can find in Lua, simple Lua. I'll talk again about data types later, in the ROBLOX API section, to explain the ROBLOX-specific data types.

Numbers

You probably already know what a number is. A number is a value composed of digits that represents a quantity. That's true in Lua too. And I won't have much to explain, as they work the same way as the numbers you already know.

In Lua, you can create numbers the same way as the way you learnt in school. In fact, you can use decimal points, decimal exponents and you can even write your numbers in hexadecimal, if you prefix them with 0x (hexadecimal is the base 16, it is a different way to express numbers. If you don't know about it, don't worry, you don't need to know about it to learn Lua).

Here are some valid numbers:

3 3.0 3.1416 314.16e-2 0.31416E1 0xff 0x56

You might not know what some of these are, but that doesn't matter.

Strings

Strings are simple to understand, they are text. Yes, text. A string can contain any form of text.

Strings can be delimited by single or double quotes ('' and "", respectively). They can contain the following escape characters (and some others that I won't bother mentionning):

Escape Characters
Character Description
\n A newline.
\" A double quote (")
\' A single quote (or apostrophe) (')
\\ A backslash (\)
\t An horizontal tab
\[number] [number] must be a number from 0 to 255. The result will be the ASCII character with the corresponding code.

You can use these in strings to reproduce characters that, if put in the string under their normal form, might cause a problem.

Here is an example of using the escaped characters:

"this is a string with \" quotes and \n a new line, as well as some backslashes \\"

[...]

Variables

I've talked about variables not long ago. But, what are variables? Well, variables are used to store data. Each variable has its own name. Since Lua is a dynamically typed (which means that values have a type, but that variables don't), you can store any value in a variable, even if its type doesn't correspond with the previous value.

To store a value in a variable, you use the '=' sign. Here is an example:

variable = 5

This will set the value of 'variable' to 5.