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

Write your first game in Lua — even if you've never programmed

We create our first game "Guess the number" in Lua — step by step, clearly and with a cheerful result.

К

Kodik

Author

4 min read

Lua is an easy and fast programming language that is often used in games, applications, and even microcontrollers. Its syntax is very simple, so it is ideal for beginners. Lua is used in well-known projects: for example, in Roblox, Angry Birds, World of Warcraft and many other games.

🎮 What we will do in this article

Together we will create a simple but exciting the game "Guess the number". This is a text game in which the computer will guess a random number, and you (or the player) will try to guess it. All this is done using basic Lua commands. This game will help you understand how they work variables, terms, cycles and user data input.

The program will be fully functional, and you will be able not only to run it, but also to change it for yourself or add to it. Ready? Let's go! 🚀

🧩 Why Lua is perfect for beginners and game devs

  • Simple syntax — reads almost like plain text

  • Minimum of excess — you immediately write the logic, without unnecessary "binding"

  • Instant start — copy, paste and run online

  • Really used in games — for example, in Roblox and Defold

🔥 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

🛠️ How to build a simple game step by step

We suggest starting with the classics — games "Guess the number".

What the game does:

  1. The computer guesses a number from 1 to 10

  2. The player tries to guess in 3 attempts

  3. The game tells you whether you won or not

Ready-made code (can be inserted into Replit):

math.randomseed(os.time())

local secret = math.random(1, 10)
local attempts = 3

print("Hi! I thought of a number from 1 to 10.")
print("You have " .. attempts .. " attempts to guess!")

while attempts > 0 do
  io.write("Enter number: ")
  local guess = tonumber(io.read())

  if guess == secret then
    print("🎉 Hurray! You guessed it!")
    break
  else
    attempts = attempts - 1
    if attempts > 0 then
      print("❌ Incorrect. Attempts left: " .. attempts)
    else
      print("😢 Alas, you lost. The number was: " .. secret)
    end
  end
end

🔍 Detailed analysis of the code of the game "Guess the number"

math.randomseed(os.time())

⏱️ This line "seeds" the random number generator with the current time. This is necessary so that the game guesses a new number each time. Without it, the program will select the same number each time it is run.

local secret = math.random(1, 10)

🎯 Using math.random we select a random number from 1 to 10. This is the number that the player has to guess.

local attempts = 3

📉 Set the number of attempts — in our case, three.

print("Hi! I thought of a number from 1 to 10.")
print("You have " .. attempts .. " attempts to guess!")

💬 Display a greeting and instructions. The .. operator combines strings and variables.

while attempts > 0 do

🔁 Run the cycle that will be executed while the player has at least one attempt.

  io.write("Enter number: ")
  local guess = tonumber(io.read())

⌨️ Request input. io.read() reads the line, and tonumber() turns it into a number.

  if guess == secret then
    print("🎉 Hurray! You guessed it!")
    break

✅ If the user guessed the number — congratulations and exit the loop with break.

  else
    attempts = attempts - 1

❌ If you didn't guess correctly, we reduce the number of remaining attempts.

    if attempts > 0 then
      print("❌ Incorrect. Attempts left: " .. attempts)

📢 We inform you how many attempts are left.

    else
      print("😢 Alas, you lost. The number was: " .. secret)
    end
  end
end

🏁 If the attempts are over, display a message about the loss and show the correct number.

💡 What you learn while writing this game:

  • Work with variables: secret, attempts, guess

  • Use input/output: io.read(), print()

  • Write conditions: if / else, ==

  • Work with cycles: while

  • Use random numbers: math.random

🎮 All this is in the format of a real game that you can run, change and improve!

If you want to learn programming in a human way - without stress, with clear tasks and support - come to Code.

  • 📚 Step-by-step courses in Lua, Python, and JavaScript

  • 🤖 AI support if something is unclear

  • 💬 Telegram channel, where beginners and experienced help each other

🎯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