Learning Lua Step-By=Step

This entry is part 2 of 24 in the series Learning Lua Step-By-Step

Post Stastics

  • This post has 2014 words.
  • Estimated read time is 9.59 minute(s).


Lua is a powerful, lightweight, and easy-to-learn programming language. It was originally designed for extending applications, but it has grown to become a general-purpose language used in a wide range of projects, from game development to web applications. Lua is known for its simplicity, flexibility, and efficiency, making it a great choice for beginners to start their programming journey.

Why Learn Lua

There are several reasons why learning Lua can be a great choice, especially for ages 10 and up:

  1. Simplicity: Lua has a very small and clean syntax, which makes it easy to understand and learn, even for beginners.
  2. Versatility: Lua can be used in a variety of applications, from games and mobile apps to embedded systems and web development. This makes it a valuable skill to have.
  3. Performance: Lua is designed to be fast and efficient, making it suitable for a wide range of projects.
  4. Embeddable: Lua can be easily embedded into other applications, allowing you to extend and customize them.
  5. Community: Lua has a growing and supportive community of developers, providing resources, tutorials, and libraries to help you learn and build your projects.

Getting Ready

Before getting started learning Lua you will need to install Lua and a Lua IDE (Integrated Development Environment)/Editor. Rather than place that content here, I’ve created a separate post on installing the required tools. All these tools are free and Open Source. However, if you can, I suggest you donate time or money to the developers of these tools to motivate them to continue developing these great tools! If you can’t donate either because of age or financial circumstances, then at least drop them a note or comment on their support blogs, telling them how much you appreciate their work. Sometimes, these messages are even more motivating than money! And they are always received with gratitude!

Ok, so head over to: https://www.coderancher.us/2024/04/08/getting-ready-to-learn-lua-step-by-step/ and follow the directions there to get your toolchain installed on Windows, MacOS, and Linux.

Once you have things installed, come back here and open ZeroBrane Studio. Navigate to the projects/Lua/tutorial directory and then create a new sub-directory named “tut01”. In this directory, create a new file named “tut01.lua”. You will add all the code below to this file. Be sure to experiment and play around with the code. This is how you learn!

You can run the code from ZeroBrane Studio by pressing the green right-facing triangle or opening a terminal in the same directory as your lua file and typing lua <filename>. For our tuto1.lua file this would be:

> lua tut01.lua

Note: the ‘>’ character represents the terminal prompt.

Be sure to run the code each time you add or change the code. Ok, let’s start learning Lua!


In Lua, you can use two types of comments:

  1. Single-line comments: To create a single-line comment, start the line with two hyphens (--). Anything after the two hyphens on that line will be ignored by the Lua interpreter.
-- This is a single-line comment.
  1. Multi-line comments: To create a multi-line comment, enclose your text between --[[ and ]]. Anything between these delimiters will be ignored by the Lua interpreter.
This is a

Comments are important for documenting your code and making it more readable and maintainable. They can help you explain the purpose and logic of your code, especially when you come back to it later or share it with others.

When you first start programming you tend to use too few comments. Then after coding for a little while you will begin to use too many comments in your code. After gaining some experience, you will eventually learn to add comments to explain difficult-to-understand code or edge and corner cases for your code. While comments are almost a requirement for new programmers, expert programmers often find they clutter up the code and are rarely kept up to date. And, so expert developers use few comments. But, as a beginner, you’ll want to comment on anything you may find difficult to understand or remember after a few days or weeks away from your code. So as a novice, don’t be afraid to use liberal comments in your code.

Outputting to the Console/Terminal

In Lua, you can use the print() function to output text to the console or terminal. The print() function can take one or more arguments, which will be concatenated and displayed as a single output.

print("Hello, World!")
print("The answer is", 42)

This will output:

Hello, World!
The answer is    42

The print() function is a handy way to display information and debug your code while you’re learning and developing.

Data Types

What is a Data Type?

In programming, a data type is a classification of data that determines the type of values the data can hold, the operations that can be performed on that data, and the way the data is stored in memory.

Lua has several built-in data types that you can use in your programs. Understanding these data types is essential for working with Lua effectively.


Strings are used to represent text or a sequence of characters. In Lua, strings can be enclosed in single quotes ('), double quotes ("), or square brackets ([[]]).

name = "Alice"
message = 'Hello, world!'
multiline = [[This is a

Strings can be concatenated using the .. operator.

greeting = "Hello, " .. name .. "!"


Lua has a single numeric data type called “number”, which can represent both integers and floating-point values.

age = 12
pi = 3.14159

Lua can perform a variety of mathematical operations on numbers, such as addition, subtraction, multiplication, division, and more.

sum = age + 5
product = age * 2


Booleans are a data type that can have one of two values: true or false. Booleans are often used in conditional statements and logical operations.

isStudent = true
isAdult = age >= 18


Lua has a unique data type called nil, which represents the absence of a value. nil is used to indicate that a variable has no value assigned to it, or that a table key does not have an associated value. nil is an important concept in Lua, as it allows you to differentiate between a variable that has not been assigned a value and one that has been explicitly set to nil. When working with Lua, you’ll often need to test if a variable is nil to ensure your code behaves as expected. You can use the == operator to check if a variable is nil, like this:

if myVariable == nil then ... end

Alternatively, you can use the type() function to check the data type of a variable, which will return "nil" if the variable is nil. Understanding nil and how to handle it is crucial for writing robust and reliable Lua code.

Finding Data Type

Lua’s type() function is a powerful tool for determining the data type of a variable or value. The type() function takes a single argument and returns a string representing the type of that argument. The possible return values include:

"nil", "number", "string", "boolean", "table", "function", "thread", and "userdata".

Using type() is particularly useful when you need to perform different operations based on the type of a variable. For example, you may want to handle a number differently than a string, or take a specific action if a variable is nil. You can use the return value of type() in conditional statements to branch your logic accordingly. For instance:

if type(myVariable) == "number" then 
  -- do something with the number end.

The type() function provides a robust way to dynamically inspect the types of values in your Lua code, which helps ensure your program behaves correctly in a variety of scenarios.

These are the basic data types in Lua. As you progress in your Lua learning, you’ll encounter more advanced data types and structures, but mastering these fundamental types will give you a solid foundation to build upon.


To help you solidify your understanding of the topics covered in this lesson, please complete the following exercises:

  1. Commenting Practice:
  • Create a Lua script that demonstrates the use of both single-line and multi-line comments.
  • Include comments that explain the purpose of each section of your code.
  1. Printing to the Console:
  • Write a Lua script that prints your name, age, and a fun fact about yourself to the console.
  • Experiment with different ways of using the print() function, such as printing multiple values at once.
  1. String Manipulation:
  • Create a Lua script that declares a string variable with your full name.
  • Use string concatenation to create a greeting that says “Hello, [your name]!”
  • Experiment with different ways of declaring strings, such as using single quotes, double quotes, and square brackets.
  1. Basic Arithmetic:
  • Write a Lua script that declares two numeric variables, performs basic arithmetic operations (addition, subtraction, multiplication, division) on them, and prints the results to the console.
  • Try using both integer and floating-point numbers in your calculations.
  1. Boolean Logic:
  • Create a Lua script that declares two boolean variables, one representing whether you are a student and one representing whether you are an adult (based on your age).
  • Use conditional statements to print different messages based on the values of these boolean variables.

Remember to save your Lua scripts and test them in the console or an integrated development environment (IDE) like Visual Studio Code or ZeroBrane Studio. Experimenting with the code and trying different variations will help you solidify your understanding of Lua’s data types and basic syntax.


Congratulations! You’ve completed the first step in your journey to learning Lua programming. In this lesson, you’ve learned about the fundamentals of Lua, including its simplicity, versatility, and performance advantages. You’ve also explored the basic data types in Lua, such as strings, numbers, booleans, and nil, as well as how to use comments and output to the console.

The exercises you’ve completed should have helped you reinforce your understanding of these concepts and give you practical hands-on experience with Lua programming. Remember, the more you practice and experiment with the language, the more comfortable and confident you’ll become.

As you continue your Lua learning journey, you’ll explore more advanced topics, such as control structures, functions, tables (Lua’s powerful data structure), and various Lua-based frameworks and libraries. Each new concept you learn will build upon the foundations you’ve established in this first lesson.

Keep up the great work, and don’t hesitate to seek out additional resources, such as online tutorials, coding challenges, and the vibrant Lua community, to further enhance your skills. With dedication and persistence, you’ll be well on your way to becoming a proficient Lua programmer. Enjoy the journey!

Great idea! Here’s a “Resources” section that provides a list of web resources for learning programming with Lua, including the Lua.org site and the ZeroBrane IDE from GitHub:


As you continue your journey in learning Lua, here are some valuable resources to help you along the way:

  1. Lua.org
    • The official Lua website, which provides the language’s documentation, downloads, and community resources: https://www.lua.org/
  2. The Lua Programming Language
  3. ZeroBrane Studio
  4. Lua for Beginners
  5. Lua Tutorials
  6. Lua Programming Wikibook
  7. Lua Subreddit

These resources cover a wide range of topics, from the official Lua documentation and tutorials to community-driven projects and discussion forums. Feel free to explore them as you continue to develop your Lua programming skills.

Series Navigation<< Getting Ready to Learn Lua Step-By-Step<< Learning Lua Step-By-Step (Part 11)<< Learning Lua Step-By-Step (Part 14)Learning Lua Step-By-Step (Part 2) >>

Leave a Reply

Your email address will not be published. Required fields are marked *