Courses/Lua/Introduction to Lua

    Introduction to Lua

    Discover why Lua powers game engines, embedded systems, and millions of scripts worldwide.

    What You'll Learn

    • What Lua is and where it's used in the real world
    • How to write and run your first Lua script
    • Lua's basic data types: nil, boolean, number, string
    • Variables, comments, and string concatenation

    What Is Lua?

    Lua (meaning "moon" in Portuguese) is a lightweight, high-performance scripting language designed to be embedded inside larger applications. Created in 1993 at the Pontifical Catholic University of Rio de Janeiro, it has become the most popular scripting language in the game industry.

    ๐ŸŽฎ Real-World Analogy: Think of Lua as a universal remote control. Just as a remote doesn't contain the TV itself but controls what happens on screen, Lua doesn't build entire applications โ€” it scripts the behaviour of a host program like a game engine.

    Where Lua Is Used

    • Roblox โ€” All game logic is written in Luau (a Lua dialect)
    • World of Warcraft โ€” UI addons and macros
    • Lร–VE2D โ€” A complete 2D game framework
    • Nginx / OpenResty โ€” High-performance web scripting
    • Redis โ€” Server-side scripting for database operations
    • Neovim โ€” Plugin and configuration scripting

    Hello, Lua!

    See how Lua syntax works with your first program.

    Try it Yourself ยป
    JavaScript
    // Your First Lua Program โ€” simulated in JavaScript
    console.log("=== Hello, Lua! ===");
    console.log();
    
    // In Lua you write:
    // print("Hello, World!")
    
    console.log("Lua Syntax:");
    console.log('print("Hello, World!")');
    console.log();
    console.log("Output: Hello, World!");
    console.log();
    
    console.log("=== Why Lua? ===");
    console.log("Created in 1993 at PUC-Rio, Brazil by Roberto Ierusalimschy.");
    console.log();
    const reasons = [
      "๐ŸŽฎ #1 scripting language for game engines (Roblox, Lร–VE2D, WoW)",
    
    ...

    Your First Script

    A Lua script is simply a text file (usually .lua) containing instructions. Here is the simplest possible program:

    print("Hello, World!")

    That's it โ€” no boilerplate, no imports, no main function. Lua starts executing from the first line of the file.

    To run this, save the line above in a file called hello.lua and run lua hello.lua in your terminal.

    Comments

    -- This is a single-line comment
    
    --[[
      This is a
      multi-line comment
    ]]

    Variables and Data Types

    Lua is dynamically typed, meaning you don't declare a variable's type โ€” Lua figures it out from the value you assign.

    local name = "Alice"    -- string
    local age = 25           -- number (all numbers are 64-bit floats)
    local active = true      -- boolean
    local nothing = nil      -- nil means "no value"
    
    print(type(name))        -- "string"
    print(type(age))         -- "number"

    The local keyword creates a variable scoped to the current block. Without it, the variable becomes global โ€” which is generally bad practice.

    โš ๏ธ Common Mistake

    Forgetting local creates a global variable that can be accidentally overwritten anywhere in your code. Always use local unless you have a specific reason not to.

    String Concatenation

    Lua uses .. (two dots) to join strings โ€” not + like most other languages:

    local greeting = "Hello, " .. name .. "!"
    print(greeting)  -- Hello, Alice!

    Variables & Types

    Explore Lua's data types and type checking.

    Try it Yourself ยป
    JavaScript
    // Lua Variables & Types โ€” simulated in JavaScript
    console.log("=== Lua Data Types ===");
    console.log();
    
    const types = [
      { type: "nil",      example: "local x         -- x is nil (no value)" },
      { type: "boolean",  example: "local active = true" },
      { type: "number",   example: "local score = 99.5   -- all numbers are doubles" },
      { type: "string",   example: 'local name = "Lua"' },
      { type: "table",    example: "local items = {1, 2, 3}" },
      { type: "function", example: "local greet = fu
    ...

    ๐Ÿ’ก Pro Tip

    Lua's entire standard library fits in your head โ€” there are only about 30 built-in functions. This simplicity is a feature, not a bug. It makes Lua incredibly easy to learn and fast to embed.

    ๐Ÿ“‹ Quick Reference

    ConceptSyntax
    Print outputprint("Hello")
    Local variablelocal x = 10
    String concat"a" .. "b"
    Comment-- single line
    Check typetype(x)
    Nil valuelocal y = nil

    ๐ŸŽ‰ Lesson Complete!

    You now know what Lua is, where it's used, and how to write basic scripts. Next, we'll dive deeper into variables, data types, and Lua's powerful table structure.

    Sign up for free to track which lessons you've completed and get learning reminders.

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy Policy โ€ข Terms of Service