{}const=>[]async()letfn</>var
LuaDevelopment

First code in Lua: a guide for those who want to start programming and not go crazy

Let's learn Lua from scratch — from installation to a mini-game in 10 minutes. No boring theory, just code examples and a fun presentation. Find out why Lua is one of the friendliest languages to start programming, where it is used, and how to write your first project.

К

Kodik

Author

6 min read

When you opened Lua and thought, "Wait, where are the curly braces?"

There are languages that make you want to close your laptop and go graze cows in the village. And there is Lua, a language that greets you like an old friend: without unnecessary ceremonies, without five-story constructions and without the feeling that you accidentally opened someone else's dissertation on quantum physics.

If you've ever played World of Warcraft, Roblox, or a mod on Garry's Mod, congratulations, you've already indirectly used Lua. This language is literally everywhere where you need to quickly script something without deploying a space station of dependencies.

Step 0: Installation (spoiler — it's fast)

Lua is one of the easiest languages to install. No gigabyte SDKs, no "please update your Java version to 847.3".

On Linux/macOS:

# Ubuntu/Debian
sudo apt install lua5.4

# macOS (via Homebrew)
brew install lua

On Windows:

Download LuaBinaries from official website or put through scoop:

scoop install lua

Let's check that everything works:

lua -v

If you get something like Lua 5.4.7 in response, you're in business. If not, well, it's a classic, check the PATH.

🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

Step 1: Hello, World! (initiation ritual)

Create a file hello.lua and write:

print("Hello, World!")

Launch:

lua hello.lua

That's it. One. One single call. Without public static void main, without import, without #include <iostream>. Lua respects your time.

For comparison, here is the same in Java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Lua looks at this as:

"Bro, why so many letters? print() and move on."

Step 2: Variables — just write and that's it

In Lua, you don't need to declare the type of a variable. No int, string, float. Lua will figure it out:

name = "Alexey"
age = 25
is_developer = true

print(name)          -- Алексей
print(type(age))     -- number
print(type(name))    -- string

Local variables are declared through local — and this is considered good practice:

local score = 100
local player = "Hero"

Why local? Because without it, the variable becomes global, and global variables in a large project are like leaving the keys to the apartment under the doormat. It seems convenient, but then you'll be surprised.

Step 3: Conditions — if, elseif, else, end

local hp = 30

if hp > 50 then
    print("Everything is fine, we continue to fight")
elseif hp > 0 then
    print("Be careful, HP is running out!")
else
    print("Game Over. Respawn in 5 seconds")
end

Please note: instead of { and }then and end. It's unusual at first, but then you'll realize that it's even cleaner to read. Lua loves words, not symbols.

Another point is that Lua does not have !=. Instead, ~= is used:

if name ~= "Admin" then
    print("Access denied")
end

Yes, tilde. Just accept it.

Step 4: Cycles — spin, twist

while:

local i = 1
while i <= 5 do
    print("Iteration: " .. i)
    i = i + 1
end

By the way, .. is a concatenation of strings in Lua. Not +, not concat(), but just two points.

for:

for i = 1, 5 do
    print("Step " .. i)
end
CODE_BLOCK_12__

for-in (for table traversal):

local fruits = {"apple", "banana", "mango"}

for index, fruit in ipairs(fruits) do
    print(index .. ". " .. fruit)
end

Step 5: Functions — write your own commands

local function greet(name)
    return "Hi, " .. name .. "! Welcome to Lua"
end

print(greet("Maria"))

Functions in Lua are first-class citizens. They can be stored in variables, passed as arguments, and returned from other functions. In general, functional programming is not just words here:

local function apply(func, value)
    return func(value)
end

local function double(x)
    return x * 2
end

print(apply(double, 21))  -- 42

42 is the answer to everything. Coincidence? I don't think so.

Step 6: Tables — the heart of Lua

This is where the magic begins. In Lua there are no arrays, no objects, no dictionaries. But there is tables — a universal data structure that replaces everything:

-- Как массив
local colors = {"Red", "Green", "Blue"}
print(colors[1])  -- красный (индексация с 1!)

-- Как словарь
local player = {
    name = "Warrior",
    hp = 100,
    level = 5
}
print(player.name)   -- Warrior
print(player["hp"])  -- 100

-- Как объект с методами
function player:takeDamage(damage)
    self.hp = self.hp - damage
    print(self.name .. " received " .. damage .. " damage. HP: " .. self.hp)
end

player:takeDamage(25)  -- Warrior получил 25 урона. HP: 75

Important note: indexing in Lua starts with 1, not 0. Yes, this is the very detail that will cause you an existential crisis if you are used to JavaScript or Python. But you'll get used to it.

Step 7: Mini-project — text quest

It's time to put it all together. Here's a little text game for you:

local function game()
    print("=============================")
    print("  CODE DUNGEON")
    print("=============================")
    print("")
    print("You are standing in front of the entrance to a dark cave.")
    print("A strange noise is heard inside...")
    print("")
    print("What are you doing?")
    print("1 - Enter the cave")
    print("2 - You go home to drink tea")
    print("")

    io.write("Your choice: ")
    local choice = io.read()

    if choice == "1" then
        print("")
        print("You enter and find... an ancient laptop!")
        print("A file with Lua code is open on the screen.")
        print("You start reading... and you become a programmer!")
        print("")
        print("🏆 VICTORY! +100 to programming skill")
    elseif choice == "2" then
        print("")
        print("A wise choice. Tea is also important.")
        print("But the cave will be waiting for you...")
        print("")
        print("☕ END. But you can always come back!")
    else
        print("")
        print("Unknown command. The cave is confused.")
        print("Try again!")
    end
end

game()

Save this in quest.lua, run lua quest.lua — and congratulations, you just wrote your first game in Lua.

What's next?

Lua is a language that will open the doors to game development (Love2D, Defold, Roblox), embedded development, and writing plugins and scripts for existing applications. It is light, fast and at the same time surprisingly powerful.

Here are some directions you can move in:

  • Roblox Studio - if you want to make games and earn money

  • Love2D — minimalist 2D engine for indie games

  • Neovim — configuration and plugins are written in Lua

  • OpenResty/Nginx — Lua for high-load web servers

  • Embedded/IoT - Lua works great on microcontrollers (NodeMCU, ESP8266)

If you like this format and want to move on, look into Code. This is an application where you can learn programming from scratch: from Python and JavaScript to HTML, CSS and other technologies. No dry theory on 200 pages - only clear lessons with practice, after which you really understand what you wrote and why.

Code created specifically for those who want to learn at their own pace, from a phone or computer - without being tied to a schedule and long courses.

And we also have Telegram channel with a community of 2000+ developers, where useful posts on programming, task analysis and news from the world of development are regularly published. This is a great way to repeat the material and stay on topic — even when you are standing in line for coffee.

Total

Lua is a rare case where a programming language doesn't try to intimidate you. It is minimalistic, logical and allows you to write working code literally from the first minutes of acquaintance. If you've been wanting to try something new for a long time or are looking for an easy entry into programming, Lua will be a great choice.

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card