What is Lua? 🚀
Lua is a lightweight, fast and embedded programming language created in 1993 in Brazil. It is widely used in game development, automation and scripting. Many popular games such as Roblox, World of Warcraft, and Garry's Mod, use Lua to create scripts. 🎮
Why study Lua? 🔥
Simplicity: clear syntax that is easy to learn.
Flexibility: can be used in games, web and automation.
Lightness: takes up little space and is quickly executed.
Embeddability: Lua can be embedded in other programs.
How to install Lua? 💻
To work with Lua you will need:
✅ Lua interpreter. You can download it from official website.
✅ Code editor. Recommended Visual Studio Code with the Lua extension.
How to check if Lua is installed? 🛠
Open the terminal and enter:
lua -vIf Lua is installed, the interpreter version will appear.
Lua Syntax Basics 📚
1. Variables 📦
In Lua, variables do not require an explicit type specification:
Example of declaring variables:
name = "Alice" -- Строка
age = 25 -- Число
pi = 3.14 -- Число с плавающей точкой
print("Name:", name, "Age:", age, "Pi:", pi)2. Data output 📤
To display information, use print().
Example:
print("Hello, world!")3. Conditional operators (if...else) 🧐
Allow the program to make decisions.
Example:
age = 20
if age >= 18 then
print("You're an adult!")
else
print("You're still a child.")
end4. Loops (for, while) 🔄
Loops allow you to repeat actions.
Example:
for i = 1, 5 do
print(i)
end5. Functions 📐
Functions help to structure the code.
Example:
function square(x)
return x * x
end
print("Square of the number 5:", square(5))Simple projects in Lua 💡
1. Random number generator 🎲
The program generates a random number from 1 to 100.
math.randomseed(os.time()) -- Устанавливаем случайное зерно
random_number = math.random(1, 100)
print("Random number:", random_number)2. Multiplication table 📊
The program displays a multiplication table from 1 to 10.
for i = 1, 10 do
for j = 1, 10 do
io.write(i * j, "\t")
end
print()
end3. Counting the number of characters in a string 🔢
The program counts the characters in the string.
text = "Hello, Lua!"
print("Number of characters:", #text)4. Checking the number for evenness 🔍
The program determines whether the number is even.
number = 42
if number % 2 == 0 then
print(number .. " - even number")
else
print(number .. " - odd number")
end5. Calculating the factorial of a number 🎯
This code calculates the factorial of a given number.
function factorial(n)
if n == 0 then
return 1
else
return n * factorial(n - 1)
end
end
print("Factorial 5:", factorial(5))Conclusion 🎉
Lua is a powerful programming language that is easy to learn. We have analyzed its basics: variables, input/output, conditions, loops and functions. Now you can write your first programs! 🚀
The more you practice, the better your code becomes. Experiment, try new tasks and master programming! Good luck learning Lua! 😊
