ROBLOX Game Development/Scripting
Programming, which is also sometimes called scripting in the case of programs that run inside embedded applications, is the process of writing computer programs. A programming language is a language used to give instructions to a computer through computer code that is contained in a computer program. A programming language consists of two things: a syntax, which is like grammar in English, and libraries, basic functions provided with the language. These libraries could be compared with vocabulary in English.
Lua is a powerful, fast, lightweight and embeddable scripting language. While it can be used by itself, it has been designed to be easy to embed in another application.
Lua was designed and is being maintained at the Pontifical Catholic University of Rio de Janeiro, which is located in Brazil. Its creators are Roberto Ierusalimschy, Waldemar Celes and Luiz Henrique de Figueiredo.
"Lua" (pronounced LOO-ah) means "Moon" in Portuguese. As such, it is neither an acronym nor an abbreviation, but a noun. More specifically, "Lua" is a name, the name of the Earth's moon and the name of the language. Like most names, it should be written in lower case with an initial capital, that is, "Lua". Please do not write it as "LUA", which is both ugly and confusing, because then it becomes an acronym with different meanings for different people. So, please, write "Lua" right!—Lua authors, About Lua
Lua comes from two languages that were designed by TeCGraf (a laboratory at the Pontifical Catholic University of Rio de Janeiro): DEL and Sol. DEL means "data entry language", while Sol means "simple object language" and also means sun in Portuguese, which is why the name Lua was chosen, since it means "moon" in Portuguese. It was created for Petrobras, a Brazilian oil company, but was also used in many other projects in TeCGraf, and is now used in a multitude of projects world-wide. Lua is one of the leading languages in the field of embedded game development.
One of the main advantages of Lua is its simplicity. Some companies use it exclusively because of that advantage: they think their employees would be able to work better if they could use a programming language to perform certain tasks, but they cannot afford to give to their employees a full course on a complicated programming language. Some very simple languages like Bash or Batch here would not be powerful enough to perform these tasks, but Lua is both powerful and simple. Another of the important advantages of Lua is its capacity to be embedded, which was one of the most important characteristics of it throughout all of its development. Games like or World of Warcraft or ROBLOX need to be able to embed Lua in their application so users of the application can use it.
Hello, world!
Lua can either be used embedded in an application or by itself. This book will not describe the process to install Lua on your computer, but you can execute code using codepad or the Lua demo. The first example of Lua code in this book will be the basic and traditional hello world program.
A "Hello world" program is a computer program that outputs "Hello, world" on a display device. Because it is typically one of the simplest programs possible in most programming languages, it is by tradition often used to illustrate to beginners the most basic syntax of a programming language, or to verify that a language or system is operating correctly.—Wikipedia, Hello world program
print("Hello, world!")
The code above prints the text "Hello, world!" to the output, printing referring to displaying text in the output, not to printing something on paper. It does so by calling the print function with the string "Hello, world!" as an argument. The book will explain this later.
Comments
A comment is a code annotation that is ignored by the programming language. Comments can be used to describe one or many lines of code, to document a program, to temporarily disable code, or for any other reason. They need to be prefixed by two hyphens to be recognized by Lua and they can be put either on their own line or at the end of another line:
print("This is normal code.") -- This is a comment print("This is still normal code.") -- This is a comment at the end of a line of code.
These comments are called short comments. It is also possible to create long comments, which start with a long bracket and can continue on many lines:
print("This is normal code") --[[Line 1 Line 2 ]]
Long brackets consist of two brackets in the middle of which any number of equal signs ('=') may be put. That number is called the level of the long bracket. Long brackets will continue until the next bracket of the same level, if there is one. A long bracket with no equal sign is called a long bracket of level 0. This approach makes it possible to use closing double brackets inside of long comments by adding equal signs in the middle of the two brackets. It is often useful to do this when using comments to disable blocks of code.
--[==[ This is a comment that contains a closing long bracket of level 0 which is here: ]] However, the closing double bracket doesn't make the comment end, because the comment was opened with an opening long bracket of level 2, and only a closing long bracket of level 2 can close it. ]==]
In the example above, the closing long bracket of level 0 (']]') does not close the comment, but the closing long bracket of level 2 (']==]') does.
Syntax
The syntax of a programming language defines how statements and expressions must be written in that programming language, just like grammar defines how sentences and words must be written. Statements and expressions can be respectively compared to sentences and words. Expressions are pieces of code that have a value and that can be evaluated, while statements are pieces of code that can be executed and that contain an instruction and one or many expressions to use that instruction with. For example, 3 + 5 is an expression and variable = 3 + 5 is a statement that sets the value of variable to that expression.
The entire syntax of Lua can be found in extended Backus–Naur form on the Lua website, but you wouldn't understand anything if you read it. Extended Backus–Naur Form is a metalanguage, a language used to describe another language, just like a metawebsite is a website about a website, and just like metatables, in Lua, are tables that define the behavior of other tables (you'll learn about metatables and tables later in this book). But you're not going to have to learn extended Backus–Naur form in this book, because, while a language like Lua can be described using a metalanguage, it can also be described using words and sentences, in English, and this is exactly what this book is going to do.
Since English can be used to describe another language, then it must itself be a metalanguage (because it corresponds to the definition of a metalanguage). This is indeed the case. And since the purpose of a programming language is to describe instructions, and you can do that with English, English must also be a programming language. This, in a way, is also the case. In fact, English is a language that can be used for many things. But extended Backus–Naur form is a specialized language, and programming languages are also specialized languages. Specialization is the characteristic of being very good at doing something in particular, but not being capable of doing other things. Extended Backus–Naur form is very good at describing other languages, but it cannot be used to write instructions or to communicate a message. Programming languages are very good at giving instructions, but they cannot be used to describe languages or to communicate messages.
English is capable of doing everything: describing languages, giving instructions and communicating messages. But it is not very good at doing some of these. In fact, it is so bad at giving instructions that, if it is used to give instructions to a computer, the computer won't understand anything. That's because computers need the instructions to be very precise and unambiguous.
Scripts
When a player joins a game server, his client, the ROBLOX program running on his computer, is put into communication with the game server, which is the program running on ROBLOX's own computers. The client of the player downloads the entire place from the game server. As changes are done in the game, both by the physics, by scripts and by any other factor that may cause changes, these changes are replicated to all the clients connected to the game. When these changes happen on a player's client (like moving that player's character), the changes are replicated to the server and then to each client. This is why a player will see the characters of other players move and why changes done by scripts will be visible by each player. Scripts, which can manipulate game objects, are themselves contained in objects. There are three kinds of scripts: server scripts, local scripts, core scripts and starter scripts. They respectively correspond to the class names "Script", "LocalScript", "CoreScript" and "StarterScript". Server scripts are scripts that run on the server while local scripts run on a player's client (the client in question depends on the local script's location in the game hierarchy). Core scripts and starter scripts cannot be used by game developers and are internal to ROBLOX. You can create a script by using the basic objects panel in the studio. Find the class name of the script you want and double-click on it to insert the script. Scripts will not run everywhere; while there are many places where they can run, most of the time, you will want to put them in the workspace service. Once the script is inserted, it can be edited by double-clicking on it in the explorer panel. This will open a new tab with a script editor, where code can be put. It can then be executed by closing the script editor and running the game (with the green run arrow).