Lua Cheat Sheet

Posted : admin On 1/29/2022
  1. Lua Programming Language
  2. Roblox Lua Cheat Sheet
  3. Lua Code List
Lua cheat sheet

The Lua short reference provides a concise summary of the Lua 5.1 syntax and core libraries. It is based on the Lua 5.0 short reference by Enrico Colombini. The 4th draft (updated twice Jul 2009 by ThomasLauer) corrects a few minor errors. The 3rd draft (updated Feb 2008 by ThomasLauer) corrects a couple very minor glitches and makes clear that the short reference can be used and distributed. Local web = require('web') function printtable(t) for k,v in pairs(t) do print(k,v) end end results = web.query('Lua') printtable(results). Lua programming language cheat sheet. GitHub Gist: instantly share code, notes, and snippets. Let us now see the basic structure of Lua program, so that it will be easy for you to understand the basic building blocks of the Lua programming language. A Lua program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.

This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.

Programming in Lua
Part I. The LanguageChapter 3. Expressions

3.6 – Table Constructors

Constructors are expressions that create andinitialize tables.They are a distinctive feature of Luaand one of its most useful and versatile mechanisms.

Lua Cheat Sheet

The simplest constructor is the empty constructor,{}, which creates an empty table;we saw it before.Constructors also initialize arrays(called also sequences or lists).For instance, the statementwill initialize days[1] with the string 'Sunday'(the first element has always index 1, not 0),days[2] with 'Monday',and so on:

Constructors do not need to use only constant expressions.We can use any kind of expression for the value of each element.For instance, we can build a short sine table as

To initialize a table to be used as a record,Lua offers the following syntax:which is equivalent to

No matter what constructor we use to create a table,we can always add and remove other fields of any type to it:That is, all tables are created equal;constructors only affect their initialization.

Every time Lua evaluates a constructor,it creates and initializes a new table.Consequently, we can use tables to implement linked lists:This code reads lines from the standard inputand stores them in a linked list, in reverse order.Each node in the list is a table with two fields:value, with the line contents,and next, with a reference to the next node.The following code prints the list contents:(Because we implemented our list as a stack,the lines will be printed in reverse order.)Although instructive,we hardly use the above implementation in real Lua programs;lists are better implemented as arrays,as we will see in Chapter 11.

We can mix record-style and list-styleinitializations in the same constructor:The above example also illustrates how we can nest constructorsto represent more complex data structures.Each of the elements polyline[1], ..., polyline[4]is a table representing a record:

Those two constructor forms have their limitations.For instance,you cannot initialize fields with negative indices,or with string indices that are not proper identifiers.For such needs, there is another, more general, format.In this format,we explicitly write the index to be initialized as an expression,between square brackets:That syntax is more cumbersome, but more flexible too:Both the list-style and the record-style forms are specialcases of this more general one.The constructoris equivalent toand the constructoris equivalent to

For those that really want their arrays starting at 0,it is not difficult to write the following:Now, the first value, 'Sunday', is at index 0.That zero does not affect the other fields,but 'Monday' naturally goes to index 1,because it is the first list value in the constructor;the other values follow it.Despite this facility,I do not recommend the use of arrays starting at 0 in Lua.Remember that most functions assume thatarrays start at index 1,and therefore will not handle such arrays correctly.

You can always put a comma after the last entry.These trailing commas are optional, but are always valid:Such flexibility makes it easier to write programs that generate Lua tables,because they do not need to handle the last element as a special case.

Finally, you can always use a semicolon instead of a comma in a constructor.We usually reserve semicolons to delimitdifferent sections in a constructor,for instance to separate its list part from its record part:

Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.
  • Lua Tutorial
  • Lua Basics Tutorial
  • Lua Advanced
  • Lua Libraries
  • Lua Useful Resources
  • Selected Reading

Let us start creating our first Lua program!

First Lua Program

Interactive Mode Programming

Lua provides a mode called interactive mode. In this mode, you can type in instructions one after the other and get instant results. This can be invoked in the shell by using the lua -i or just the lua command. Once you type in this, press Enter and the interactive mode will be started as shown below.

You can print something using the following statement −

Once you press enter, you will get the following output −

Default Mode Programming

Invoking the interpreter with a Lua file name parameter begins execution of the file and continues until the script is finished. When the script is finished, the interpreter is no longer active.

Let us write a simple Lua program. All Lua files will have extension .lua. So put the following source code in a test.lua file.

Assuming, lua environment is setup correctly, let’s run the program using the following code −

We will get the following output −

Lua Programming Language

Let's try another way to execute a Lua program. Below is the modified test.lua file −

Here, we have assumed that you have Lua interpreter available in your /usr/local/bin directory. The first line is ignored by the interpreter, if it starts with # sign. Now, try to run this program as follows −

We will get the following output.

Let us now see the basic structure of Lua program, so that it will be easy for you to understand the basic building blocks of the Lua programming language.

Tokens in Lua

Lua tutorial

A Lua program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. For example, the following Lua statement consists of three tokens −

The individual tokens are −


Comments are like helping text in your Lua program and they are ignored by the interpreter. They start with --[[ and terminates with the characters --]] as shown below −


Roblox Lua Cheat Sheet

A Lua identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter ‘A to Z’ or ‘a to z’ or an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9).

Lua does not allow punctuation characters such as @, $, and % within identifiers. Lua is a case sensitive programming language. Thus Manpower and manpower are two different identifiers in Lua. Here are some examples of the acceptable identifiers −


The following list shows few of the reserved words in Lua. These reserved words may not be used as constants or variables or any other identifier names.


Whitespace in Lua

Lua Code List


A line containing only whitespace, possibly with a comment, is known as a blank line, and a Lua interpreter totally ignores it.

Whitespace is the term used in Lua to describe blanks, tabs, newline characters and comments. Whitespace separates one part of a statement from another and enables the interpreter to identify where one element in a statement, such as int ends, and the next element begins. Therefore, in the following statement −

There must be at least one whitespace character (usually a space) between local and age for the interpreter to be able to distinguish them. On the other hand, in the following statement −

No whitespace characters are necessary between fruit and =, or between = and apples, although you are free to include some if you wish for readability purpose.