Showing posts with label lua. Show all posts
Showing posts with label lua. Show all posts

lua 12

Chapter 12 — Objects (OOP) ๐Ÿง™‍♂️ (The "Real Games" Chapter)

Forget the scary term "Object-Oriented Programming".

You don't need to remember the definition.

Just remember this:

An Object = A real thing with data + actions.

That's it.


๐ŸŽฏ Mission

Imagine Minecraft.

There is a player.

The player has:

  • Name

  • Health

  • Coins

  • Level

The player can:

  • Jump

  • Attack

  • Heal

  • Die

Notice something?

The player has information AND actions.

That's an object.


Real-life Examples

๐Ÿš— Car

Has:

  • Speed

  • Color

  • Fuel

Can:

  • Drive

  • Brake

  • Turn


๐Ÿ‘ค Player

Has:

  • Health

  • Coins

  • Name

Can:

  • Jump

  • Attack

  • Heal


๐Ÿถ Dog

Has:

  • Name

  • Age

Can:

  • Bark

  • Run


You already know almost everything needed.


๐ŸŸข LEVEL 1 — Survival


Q1

Which is an object?

A) Player

B) Health

C) Number

D) String

Answer: A

๐Ÿ’ก A player is a real "thing."


Q2

A Player has...

A) Health

B) Coins

C) Name

D) All of the above

Answer: D


Q3

A Player can...

A) Jump

B) Attack

C) Heal

D) All of the above

Answer: D


Q4

Which belongs inside a Player object?

A) Health

B) Coins

C) Inventory

D) All of the above

Answer: D


Q5

What is "Jump"?

A) Data

B) Action

C) Number

D) Table

Answer: B

๐Ÿ’ก Jump is something the player does.


Q6

What is "Health"?

A) Action

B) Data

C) Loop

D) Function

Answer: B


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

Which looks most like a Player?

A)

Player
Health
Coins
Jump()
Attack()

B)

Player

C)

Health

D)

Jump()

Answer: A


Q8

You're making GTA.

Which should be an object?

A) Car

B) Player

C) Police

D) All of the above

Answer: D


Q9

Which is an action?

A) Name

B) Coins

C) Shoot()

D) Health

Answer: C


Q10

Which is data?

A) Attack()

B) Jump()

C) Coins

D) Heal()

Answer: C


Q11

Inventory belongs to...

A) Player

B) Weather

C) Music

D) Menu

Answer: A


Q12

Which pair is correct?

A)

Health → Data
Attack() → Action

B)

Attack() → Data
Health → Action

C)

Coins → Action

D)

Jump() → Data

Answer: A


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

You're building Pokรฉmon.

Which should be an object?

A) Pokรฉmon

B) Trainer

C) Item

D) All of the above

Answer: D


Q14

You're building WhatsApp.

Which is an object?

A) User

B) Chat

C) Message

D) All of the above

Answer: D


Q15

You're building YouTube.

Which is an object?

A) Video

B) User

C) Playlist

D) All of the above

Answer: D


Q16

You're building an online shop.

Which is an object?

A) Product

B) Customer

C) Order

D) All of the above

Answer: D


Q17

You're building Chess.

Which is an object?

A) King

B) Queen

C) Pawn

D) All of the above

Answer: D


Q18

Which mindset is best?

A) Think in terms of real-world things

B) Think only about variables

C) Think only about loops

D) Think only about functions

Answer: A


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

A Player object stores only actions.

What's missing?

A) Data

B) Loops

C) Tables

D) Strings

Answer: A


Q20

A Car object has:

  • Color

  • Speed

  • Fuel

But no drive().

Problem?

A) Missing action

B) Missing table

C) Missing loop

D) Nothing

Answer: A


Q21

Someone stores Player health inside Enemy.

Problem?

A) Wrong object

B) Missing loop

C) Missing variable

D) Nothing

Answer: A


Q22

Which object owns attack()?

A) Player

B) Enemy

C) Both can

D) Neither

Answer: C


Q23

Someone puts Weather inside Inventory.

Good idea?

A) Yes

B) No

C) Required

D) Lua demands it

Answer: B


Q24

A huge object contains:

  • Player

  • Enemy

  • Car

  • Shop

  • Weather

Problem?

A) Too many responsibilities

B) Too many loops

C) Too many tables

D) Nothing

Answer: A


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

An object is best described as...

A) A real thing with data and actions

B) A loop

C) A number

D) A string

Answer: A


Q26

Which pair belongs together?

A)

Player
Health
Attack()

B)

Player
Weather
Rain()

C)

Inventory
Cloud

D)

Music
Health

Answer: A


Q27

You're making Roblox.

Which should be objects?

A) Players

B) NPCs

C) Weapons

D) All of the above

Answer: D


Q28

Experienced programmers usually think...

A)

What variables do I need?

B)

What objects exist in this world?

C)

Which loop first?

D)

Which print first?

Answer: B

๐Ÿ’ก This is one of the biggest mindset shifts.


Q29

What's the biggest benefit of OOP?

A) Code matches real life

B) Easier to organize

C) Easier to expand

D) All of the above

Answer: D


Q30 — Final Boss ๐Ÿ‘‘๐Ÿ†

You're making Minecraft.

Which plan is most professional?

A)

One file
1000 variables
500 functions
Everything mixed together

B)

Player Object
Enemy Object
Block Object
Inventory Object
World Object
Item Object

Answer: B

๐Ÿ’ก Professional game code is usually organized around the things that exist in the game, not around random variables.


๐Ÿง  Pareto Cheat Sheet

Real ThingDataActions
๐Ÿ‘ค PlayerHealth, Coins, NameJump(), Attack(), Heal()
๐Ÿš— CarFuel, Speed, ColorDrive(), Brake()
๐Ÿถ DogName, AgeBark(), Run()
๐Ÿ“ฆ InventoryItemsAddItem(), RemoveItem()
๐Ÿ‘น EnemyHealth, DamageAttack(), Die()

๐ŸŽฏ The Biggest Programming Mindset

Forget the word OOP.

Just ask:

"What things exist in my app?"

If you're making:

๐ŸŽฎ Minecraft

  • Player

  • Block

  • Item

  • World

  • Zombie

๐Ÿ“ฑ WhatsApp

  • User

  • Chat

  • Message

  • Group

๐Ÿ›’ Amazon

  • Product

  • Customer

  • Cart

  • Order

  • Payment

Those become your objects.


๐Ÿ† Congratulations!

You now know the 20% of Lua that you'll use 80% of the time:

✅ Variables → Store one value
✅ If → Make decisions
✅ Loops → Repeat work
✅ Functions → Reuse code
✅ Tables → Store collections
✅ Strings → Handle text
✅ Modules → Organize code
✅ Objects → Model real-world things

What's next?

If your goal is Roblox, Love2D, or game scripting, don't dive into advanced Lua theory yet. Instead, start building tiny projects:

  1. ๐ŸŽฒ Number Guessing Game

  2. ๐Ÿงฎ Calculator

  3. ๐ŸŽ’ Inventory System

  4. ⚔️ RPG Player & Enemy

  5. ๐Ÿ›’ Shopping Cart

  6. ๐Ÿฆ Bank Account Simulator

  7. ๐ŸŽฎ Tic-Tac-Toe

  8. ๐Ÿ‘พ Simple 2D Game

You'll learn far more by building these than by reading another 300 pages of theory.


๐ŸŽ One correction to your learning path

If I were redesigning this course from scratch, I'd actually move Objects (OOP) before Modules. It's easier to think:

Create the thing (object) → Then put it in its own file (module).

That order tends to click faster for beginners. But now that you've finished all 12 chapters, you have the core mental model needed to start writing real Lua programs.

lua 11

Chapter 11 — Modules (๐Ÿ“ฆ Split Big Programs into Small Files)

This chapter teaches one of the biggest habits of professional programmers.

Imagine you're building Minecraft.

Would you put 50,000 lines of code into one file?

❌ Never.

Instead:

Player.lua
Inventory.lua
Enemy.lua
Shop.lua
Save.lua
Settings.lua

Each file has one job.

That's what Modules are.


๐ŸŽฏ Mission

Imagine building an online shopping app.

Would you rather have:

Everything.lua

(30,000 lines ๐Ÿ˜ฑ)

OR

Login.lua
Cart.lua
Products.lua
Payment.lua
Orders.lua

Much cleaner.


๐ŸŸข LEVEL 1 — Survival


Q1

What is the main purpose of modules?

A) Store numbers

B) Split code into smaller files

C) Create loops

D) Print text

Answer: B


Q2

Why do programmers use modules?

A) To organize code

B) To make code longer

C) To replace variables

D) To replace loops

Answer: A


Q3

Imagine a game.

Where should inventory code go?

A) Inventory module

B) Settings module

C) Login module

D) Music module

Answer: A


Q4

A module is basically...

A) A separate Lua file

B) A loop

C) A table

D) A string

Answer: A


Q5

Which project is easier to manage?

A)

Everything.lua

B)

Player.lua
Enemy.lua
Inventory.lua

Answer: B


Q6

The biggest benefit of modules is...

A) Better organization

B) Faster internet

C) More RAM

D) Bigger monitor

Answer: A


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

You're making a racing game.

Where should car movement code go?

A) Car.lua

B) Music.lua

C) Shop.lua

D) Save.lua

Answer: A


Q8

You're making a chat app.

Where should login code go?

A) Login.lua

B) Message.lua

C) Theme.lua

D) Emoji.lua

Answer: A


Q9

Which project looks more professional?

A)

game.lua

(10,000 lines)

B)

Player.lua
Enemy.lua
Map.lua
Items.lua

Answer: B


Q10

Modules make code...

A) Easier to find

B) Harder to read

C) Slower

D) More confusing

Answer: A


Q11

If a bug is in inventory,

where do you look first?

A) Inventory.lua

B) Player.lua

C) Music.lua

D) Random file

Answer: A


Q12

A module should usually have...

A) One main responsibility

B) Ten unrelated jobs

C) No purpose

D) Every function in the project

Answer: A

๐Ÿ’ก This is called the Single Responsibility Principle.


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

You're making YouTube.

Where should video-playing code go?

A) VideoPlayer.lua

B) Login.lua

C) Comments.lua

D) Settings.lua

Answer: A


Q14

You're making Instagram.

Where should photo upload code go?

A) Upload.lua

B) Chat.lua

C) Theme.lua

D) Friends.lua

Answer: A


Q15

You're making WhatsApp.

Where should message code go?

A) Messages.lua

B) Camera.lua

C) Login.lua

D) Settings.lua

Answer: A


Q16

You're making a game.

Enemy AI belongs in...

A) Enemy.lua

B) Player.lua

C) Shop.lua

D) Audio.lua

Answer: A


Q17

A shopping cart should probably live in...

A) Cart.lua

B) Music.lua

C) Weather.lua

D) Login.lua

Answer: A


Q18

Which project is easiest to expand?

A) Many small modules

B) One huge file

C) One huge function

D) Thousands of variables

Answer: A


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

Your file contains:

  • Login

  • Shop

  • Music

  • Inventory

  • Save

  • Enemy AI

Problem?

A) Too many unrelated jobs

B) Missing loop

C) Missing variable

D) Nothing

Answer: A


Q20

You can't find inventory code.

Why?

A) Everything is in one file

B) Missing print

C) Missing for

D) Missing table

Answer: A


Q21

Someone names files:

a.lua
b.lua
c.lua

What's wrong?

A) Poor names

B) Too many loops

C) Too many strings

D) Nothing

Answer: A

๐Ÿ’ก Good names save hours of searching.


Q22

Which is better?

A)

Enemy.lua

B)

thing.lua

C)

abc.lua

D)

random.lua

Answer: A


Q23

A file grows to 8,000 lines.

Best idea?

A) Split it into modules

B) Keep adding more code

C) Delete comments

D) Rename variables

Answer: A


Q24

Which naming style is best?

A)

Player.lua

B)

stuff.lua

C)

test2.lua

D)

file.lua

Answer: A


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

A module is...

A) A well-organized Lua file with a specific purpose

B) A loop

C) A table

D) A number

Answer: A


Q26

Why split code into modules?

A) Easier reading

B) Easier debugging

C) Easier teamwork

D) All of the above

Answer: D


Q27

You're making a 2D game.

Which structure looks best?

A)

Everything.lua

B)

Player.lua
Enemy.lua
Map.lua
Inventory.lua
UI.lua
Audio.lua

Answer: B


Q28

Which file should contain save-game logic?

A) Save.lua

B) Shop.lua

C) Audio.lua

D) Menu.lua

Answer: A


Q29

Professional programmers usually prefer...

A) Many small files

B) One gigantic file

C) One gigantic function

D) Thousands of global variables

Answer: A


Q30 — Final Boss ๐Ÿ†

You're building the next GTA game.

Which mindset is the best?

A)

Put every line of code into game.lua

B)

Split the project into modules based on what each part does

C)

One function with 20,000 lines

D)

One variable for everything

Answer: B

๐Ÿ’ก Big software is built by breaking big problems into small pieces. Modules are one of the main tools that make that possible.


๐Ÿง  Pareto Cheat Sheet

SituationBest Practice
Inventory codeInventory.lua
Player codePlayer.lua
Enemy codeEnemy.lua
Save systemSave.lua
AudioAudio.lua
UIUI.lua

๐Ÿšจ The Only 20% You Actually Need to Remember

You do not need to memorize require() or advanced module syntax yet.

For now, remember just these three ideas:

  1. ๐Ÿ“ฆ One file = One responsibility

  2. ๐Ÿท️ Name files by what they do (Player.lua, Shop.lua, Inventory.lua)

  3. ✂️ If one file becomes huge, split it into modules


๐ŸŽ Preview of the Next Chapter

So far you've learned:

  • Variables → Store data

  • If → Make decisions

  • Loops → Repeat work

  • Functions → Reuse code

  • Tables → Store collections

  • Strings → Handle text

  • Modules → Organize projects

The next chapter is where these pieces come together into something much more powerful:

Chapter 12 — Object-Oriented Programming (OOP): Building Real Game Objects

After Chapter 12, you'll understand how games represent things like:

  • ๐Ÿ‘ค Player

  • ๐Ÿ‘น Enemy

  • ๐Ÿš— Car

  • ๐Ÿ—ก️ Weapon

  • ๐Ÿ“ฆ Inventory

This is the chapter where Lua starts to look like the code used in real games.

lua 10

Chapter 10 — Error Handling & Debugging (๐Ÿ› ️ The Bug Detective)

Real programmers don't spend most of their time writing code.

They spend a lot of time finding and fixing bugs.

This chapter teaches you how not to panic when your program breaks.


๐ŸŽฏ Mission

Imagine your calculator asks:

Enter a number:

The user types:

hello

Without error handling:

๐Ÿ’ฅ Program crashes

With error handling:

Invalid number.
Please try again.

Much better.


๐ŸŸข LEVEL 1 — Survival


Q1

A bug is...

A) A computer virus

B) A mistake in your code

C) A function

D) A table

Answer: B


Q2

When your program suddenly stops because of a mistake, it is called...

A) Winning

B) Crashing

C) Looping

D) Printing

Answer: B


Q3

print(10 / 2)

Output?

A) 2

B) 5

C) 20

D) Error

Answer: B


Q4

print(x)

If x was never given a value, what prints?

A) Error

B) nil

C) 0

D) false

Answer: B

๐Ÿ’ก In Lua, an undefined variable is usually nil.


Q5

What is debugging?

A) Deleting code

B) Finding and fixing mistakes

C) Creating tables

D) Making loops

Answer: B


Q6

What's usually the first thing you should do after seeing an error?

A) Panic

B) Read the error message

C) Restart your computer

D) Rewrite everything

Answer: B

๐Ÿ’ก The error message often tells you exactly where to look.


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

name = nil

print(name)

Output?

A)

nil

B)

name

C) Error

D)

0

Answer: A


Q8

coins = 100

print(coins)

Output?

A) 100

B) coins

C) nil

D) Error

Answer: A


Q9

health = nil

if health == nil then
    print("Missing")
end

Output?

A)

Missing

B) nil

C) Error

D) Nothing

Answer: A


Q10

Which value usually means "nothing exists here"?

A) 0

B) false

C) nil

D) ""

Answer: C


Q11

coins = 50

coins = nil

print(coins)

Output?

A) 50

B) nil

C) 0

D) Error

Answer: B


Q12

Which tool is most commonly used to quickly inspect a variable?

A)

print(variable)

B)

table(variable)

C)

loop(variable)

D)

math(variable)

Answer: A

๐Ÿ’ก print() is every programmer's best friend while debugging.


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

A login app crashes.

What's your first step?

A) Delete the project

B) Read the error message

C) Buy a faster computer

D) Rewrite everything

Answer: B


Q14

A player's health becomes nil.

Best thing to do?

A) Ignore it

B) Print it and investigate

C) Restart the game

D) Delete the variable

Answer: B


Q15

You're not sure what value score contains.

Best code?

A)

print(score)

B)

score()

C)

if score

D)

table(score)

Answer: A


Q16

A game crashes after collecting an item.

Best approach?

A) Read the error

B) Guess randomly

C) Rewrite the inventory

D) Restart Windows

Answer: A


Q17

A website says:

Invalid Password

Instead of crashing.

This is an example of...

A) Error handling

B) Looping

C) Tables

D) Functions

Answer: A


Q18

Which programmer usually fixes bugs faster?

A) One who reads errors

B) One who ignores them

C) One who keeps guessing

D) One who rewrites everything

Answer: A


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

print("Hello"

What's wrong?

A) Missing )

B) Missing if

C) Missing end

D) Nothing

Answer: A


Q20

function hello()

print("Hi")

Problem?

A) Missing end

B) Missing if

C) Missing for

D) Nothing

Answer: A


Q21

coins = 100

print(coin)

Problem?

A) Wrong variable name

B) Missing end

C) Missing quotes

D) Nothing

Answer: A

๐Ÿ’ก coin and coins are different variables.


Q22

inventory = {
    "Sword"
}

print(inventory[2])

Output?

A)

Sword

B)

nil

C) Error

D)

2

Answer: B


Q23

health = nil

print(health + 10)

Problem?

A) Can't do math with nil

B) Missing loop

C) Missing table

D) Nothing

Answer: A


Q24

Which habit helps prevent bugs?

A) Meaningful variable names

B) Random variable names

C) Huge functions

D) Copy-pasting everything

Answer: A


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

What is the first thing you should read after a crash?

A) Comments

B) Error message

C) Function names

D) Tables

Answer: B


Q26

Which value usually means missing data?

A) 0

B) false

C) nil

D) ""

Answer: C


Q27

What's the easiest way to check a variable while debugging?

A)

print(variable)

B)

loop(variable)

C)

table(variable)

D)

math(variable)

Answer: A


Q28

Which mistake is most common?

A)

coins

vs

coin

B)

print()

C)

if

D)

end

Answer: A

๐Ÿ’ก Tiny spelling mistakes cause lots of bugs.


Q29

Which programmer improves the fastest?

A) Never makes mistakes

B) Makes mistakes and learns from them

C) Copies code without understanding

D) Avoids coding

Answer: B


Q30 — Final Boss ๐Ÿ†

Your program crashes.

What's the best order?

A)

Panic → Rewrite Everything

B)

Ignore → Hope it works

C)

Read Error
↓
Understand It
↓
Use print()
↓
Fix One Problem
↓
Test Again

D)

Restart Computer

Answer: C

๐Ÿ’ก This is how experienced programmers debug. They don't magically know the answer—they narrow the problem down step by step.


๐Ÿง  Pareto Cheat Sheet

SituationWhat to Do
Program crashesRead the error message
Variable looks wrongprint(variable)
Value doesn't existnil
Typo (coin vs coins)Check spelling
Missing end or )Read the error location
Fix bugsOne at a time

๐Ÿ† The #1 Rule of Debugging

Most beginners think:

"My code is broken."

Experienced programmers think:

"Which single line is causing the problem?"

That mindset is the difference.


๐ŸŽ Bonus: The 5 Most Common Beginner Bugs (You'll See These Again and Again)

  1. ❌ Using = instead of ==

  2. ❌ Forgetting end

  3. ❌ Misspelling variable names (coin vs coins)

  4. ❌ Accessing a table item that doesn't exist (inventory[10])

  5. ❌ Trying to do math with nil

If you can recognize these five quickly, you'll solve a large percentage of the errors you'll encounter as a beginner.

lua 9

Chapter 9 — Tables with Names (Key-Value Tables) ⭐⭐⭐⭐⭐

If Chapter 5 taught you tables...

This chapter teaches you REAL tables.

This is probably the most important Lua chapter after variables, if, loops and functions.

Almost every real Lua project uses this.


๐ŸŽฏ Mission

Imagine you're storing a player.

Instead of this:

player = {
    "Pramod",
    25,
    100,
    500
}

Question:

What is player[3]?

๐Ÿค” Health?
๐Ÿค” Coins?
๐Ÿค” Level?

Nobody knows.

Instead...

player = {
    name = "Pramod",
    level = 25,
    health = 100,
    coins = 500
}

Now it's obvious.

print(player.health)

Everyone immediately understands.

This is how real Lua code is written.


๐ŸŸข LEVEL 1 — Survival


Q1

Which player is easier to understand?

A)

player = {
    "Pramod",
    25,
    100
}

B)

player = {
    name = "Pramod",
    level = 25,
    health = 100
}

Answer: B

๐Ÿ’ก Named values are much easier to read.


Q2

Which is called a key?

player = {
    health = 100
}

A) 100

B) health

C) player

D) =

Answer: B


Q3

Which is the value?

player = {
    health = 100
}

A) health

B) player

C) 100

D) {}

Answer: C


Q4

How do you read the player's health?

A)

player.health

B)

player(health)

C)

health.player

D)

player.health()

Answer: A


Q5

player = {
    name = "Pramod"
}

print(player.name)

Output?

A)

Pramod

B)

name

C)

player

D)

nil

Answer: A


Q6

player = {
    level = 10
}

print(player.level)

Output?

A) 1

B) 10

C) level

D) nil

Answer: B


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

player = {
    health = 100
}

player.health = 80

print(player.health)

Output?

A) 100

B) 80

C) nil

D) Error

Answer: B


Q8

car = {
    brand = "Tesla",
    year = 2025
}

print(car.brand)

Output?

A)

Tesla

B)

2025

C)

brand

D) nil

Answer: A


Q9

dog = {
    age = 5
}

dog.age = dog.age + 1

print(dog.age)

Output?

A) 5

B) 6

C) 1

D) Error

Answer: B


Q10

phone = {
    battery = 75
}

print(phone.camera)

Output?

A) 75

B) 0

C) nil

D) camera

Answer: C

๐Ÿ’ก The key doesn't exist.


Q11

Which is easier?

A)

player[4]

B)

player.health

Answer: B


Q12

user = {
    username = "Alex",
    coins = 500
}

print(user.coins)

Output?

A) Alex

B) 500

C) coins

D) nil

Answer: B


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

You're making an RPG player.

Which is better?

A)

player = {
    "Pramod",
    20,
    100,
    300
}

B)

player = {
    name="Pramod",
    level=20,
    health=100,
    coins=300
}

Answer: B


Q14

A car has:

  • Brand

  • Speed

  • Fuel

Best choice?

A)

car = {
    brand="BMW",
    speed=120,
    fuel=60
}

B)

car = {
    "BMW",
    120,
    60
}

Answer: A


Q15

A website stores a user.

Best structure?

A)

user = {
    name="John",
    email="john@mail.com",
    age=25
}

B)

user = {
    "John",
    "john@mail.com",
    25
}

Answer: A


Q16

Minecraft stores a player.

Would it likely use named keys?

A) Yes

B) No

Answer: A


Q17

Roblox stores:

  • Health

  • WalkSpeed

  • JumpPower

Best tool?

A) Key-value table

B) Variables only

C) Loop

D) If

Answer: A


Q18

A bank account stores:

  • Owner

  • Balance

  • Account Number

Best approach?

A) Named table

B) Three unrelated variables

C) Loop

D) Function

Answer: A


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

player = {
    health = 100
}

print(player.Health)

Problem?

A) Lua is case-sensitive.

B) Missing loop.

C) Missing function.

D) Nothing.

Answer: A

๐Ÿ’ก healthHealth


Q20

player = {
    health = 100
}

player.health = "Dead"

Is this allowed?

A) Yes

B) No

Answer: A

๐Ÿ’ก Lua allows different data types, although using consistent types is usually better.


Q21

player = {
    coins = 500
}

print(player.gold)

Output?

A) 500

B) gold

C) nil

D) Error

Answer: C


Q22

car = {}

car.speed = 100

print(car.speed)

Output?

A) 100

B) nil

C) Error

D) speed

Answer: A

๐Ÿ’ก You can add new keys anytime.


Q23

player = {
    level = 5
}

player.level = player.level + 5

print(player.level)

Output?

A) 5

B) 10

C) nil

D) Error

Answer: B


Q24

Which is valid?

A)

player.health

B)

player->health

C)

player::health

D)

player=>health

Answer: A


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

Key-value tables make code...

A) Harder

B) Easier to read

C) Slower

D) Random

Answer: B


Q26

Which is a key?

player = {
    coins = 200
}

A) 200

B) coins

C) player

D) {}

Answer: B


Q27

How do you change health to 50?

A)

player.health = 50

B)

health.player = 50

C)

player(health)=50

D)

health = player

Answer: A


Q28

Which is easier to remember?

A)

enemy[7]

B)

enemy.damage

Answer: B


Q29

Which style do professional Lua programmers mostly use?

A)

player[1]
player[2]
player[3]

B)

player.name
player.health
player.level

Answer: B


Q30 — Final Boss ๐Ÿ†

You're creating a player for an online game.

The player has:

  • Name

  • Health

  • Mana

  • Coins

  • Level

  • Experience

  • Inventory

  • Position

Which design would an experienced Lua programmer choose?

A)

player = {
    "Pramod",
    100,
    50,
    500,
    20,
    1500,
    {},
    {10,20}
}

B)

player = {
    name = "Pramod",
    health = 100,
    mana = 50,
    coins = 500,
    level = 20,
    experience = 1500,
    inventory = {},
    position = {x=10, y=20}
}

Answer: B

๐Ÿ’ก This is how you'll see objects represented in real Lua code and game frameworks.


๐Ÿง  Pareto Cheat Sheet

NeedSyntax
Create object-like table{name="Bob", health=100}
Read valueplayer.health
Change valueplayer.health = 50
Add new valueplayer.coins = 100
Missing keynil
Nested tableplayer.position.x

๐Ÿ† Boss Tip (The Biggest Upgrade Yet)

You now know both kinds of tables:

๐Ÿ“ฆ List (ordered values)

inventory = {
    "Sword",
    "Shield",
    "Potion"
}

Access by position:

inventory[1]

Use when order matters.


๐Ÿ‘ค Object (named values)

player = {
    name = "Pramod",
    health = 100,
    level = 20
}

Access by name:

player.health

Use when each value has a meaningful name.

Real-world Lua programs use both together.

Example:

player = {
    name = "Pramod",
    health = 100,
    inventory = {
        "Sword",
        "Bow",
        "Potion"
    }
}

This combines everything you've learned:

  • Variables

  • Tables

  • Nested tables

  • Named keys

From here on, you're learning patterns used in actual Lua applications rather than just isolated language features.

lua 8

Chapter 8 — The Built-in Superpowers (๐Ÿงฐ Functions You'll Use All the Time)

Imagine buying a new phone.

You don't build your own:

  • Camera ๐Ÿ“ท

  • Calculator ๐Ÿงฎ

  • Clock ⏰

  • Flashlight ๐Ÿ”ฆ

They're already built in.

Lua is the same.

You don't write everything from scratch.

Lua already gives you powerful built-in functions.

These 6 functions cover 80% of beginner Lua programming.

  • math.random()

  • math.floor()

  • type()

  • tonumber()

  • tostring()

  • print()


๐ŸŸข LEVEL 1 — Survival


Q1

Which function prints something?

A)

show()

B)

display()

C)

print()

D)

echo()

Answer: C


Q2

Which function gives a random number?

A)

math.floor()

B)

math.random()

C)

randomNumber()

D)

type()

Answer: B

๐Ÿ’ก Used in almost every game.


Q3

print(math.random(1,6))

What does it print?

A) Always 6

B) A random number from 1 to 6

C) Always 1

D) Error

Answer: B


Q4

You're making a dice game.

Which function should you use?

A)

print()

B)

math.random()

C)

tonumber()

D)

type()

Answer: B


Q5

Which function removes the decimal part?

A)

math.floor()

B)

math.random()

C)

print()

D)

type()

Answer: A


Q6

print(math.floor(7.9))

Output?

A)

7

B)

8

C)

7.9

D) Error

Answer: A

๐Ÿ’ก Floor always rounds down.


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

print(type(100))

Output?

A)

number

B)

string

C)

boolean

D)

100

Answer: A


Q8

print(type("Lua"))

Output?

A)

number

B)

string

C)

text

D)

Lua

Answer: B


Q9

print(type(true))

Output?

A)

boolean

B)

true

C)

string

D)

number

Answer: A


Q10

print(type(nil))

Output?

A)

empty

B)

nothing

C)

nil

D)

false

Answer: C


Q11

age = "25"

print(tonumber(age))

Output?

A)

25

B)

"25"

C) Error

D)

number

Answer: A

๐Ÿ’ก Converts text into a number.


Q12

coins = 100

print(tostring(coins))

Output?

A)

100

(as a string)

B)

100

(as a number)

C) Error

D)

coins

Answer: A

๐Ÿ’ก Converts a number into text.


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

A player rolls a dice.

Best function?

A)

math.random()

B)

type()

C)

print()

D)

math.floor()

Answer: A


Q14

A game shows:

Coins: 500

Which function helps turn 500 into text?

A)

type()

B)

tostring()

C)

math.random()

D)

math.floor()

Answer: B


Q15

A user types:

"150"

You need to do math.

First step?

A)

tonumber()

B)

type()

C)

math.floor()

D)

print()

Answer: A


Q16

You want to check what kind of value a variable holds.

Best function?

A)

type()

B)

print()

C)

math.random()

D)

tostring()

Answer: A


Q17

A game calculates:

Health = 99.87

You only want:

99

Best function?

A)

math.floor()

B)

math.random()

C)

tonumber()

D)

type()

Answer: A


Q18

Which function is used in almost every Lua program?

A)

print()

B)

math.random()

C)

type()

D)

tonumber()

Answer: A


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

age = "20"

print(age + 5)

Problem?

A) Should convert using tonumber()

B) Missing end

C) Missing if

D) Nothing

Answer: A


Q20

print(math.floor("9.9"))

Is this okay?

A) Yes. Lua converts "9.9" to a number for math.floor.

B) No. Strings can never be used.

C) It prints "9.9".

D) It prints 10.

Answer: A

๐Ÿ’ก Numeric strings can be converted automatically here, but using tonumber() yourself is usually clearer.


Q21

print(type(55))

Output?

A)

55

B)

number

C)

string

D)

integer

Answer: B


Q22

print(math.random(1,1))

Output?

A)

1

B)

0

C) Random every time

D) Error

Answer: A

๐Ÿ’ก There's only one possible value.


Q23

score = 150

print("Score: " .. tostring(score))

Output?

A)

Score: 150

B)

150Score

C) Error

D)

Score

Answer: A


Q24

Which function tells you what type a value is?

A)

type()

B)

print()

C)

tonumber()

D)

math.random()

Answer: A


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

Which function creates randomness?

A)

math.floor()

B)

math.random()

C)

type()

D)

print()

Answer: B


Q26

Which converts text → number?

A)

tostring()

B)

tonumber()

C)

type()

D)

print()

Answer: B


Q27

Which converts number → text?

A)

math.random()

B)

print()

C)

tostring()

D)

type()

Answer: C


Q28

Which returns the type of a value?

A)

type()

B)

math.floor()

C)

print()

D)

random()

Answer: A


Q29

Which rounds down?

A)

math.ceil()

B)

math.floor()

C)

tonumber()

D)

type()

Answer: B


Q30 — Final Boss ๐Ÿ†

You're making an RPG game.

  • ๐ŸŽฒ Roll a dice.

  • ❤️ Show "Health: 95".

  • ๐Ÿช™ Convert "100" into a number before adding coins.

  • ๐Ÿ” Check whether a value is a string or number.

  • ๐Ÿ“‰ Remove decimals from damage.

Which set of functions covers all of these?

A)

print()

B)

math.random(), tostring(), tonumber(), type(), math.floor()

C)

if, for, while

D)

table.insert(), pairs()

Answer: B

๐Ÿ’ก These five built-in functions appear in beginner and intermediate Lua code again and again. If you know them well, you'll recognize them in almost every tutorial and project.


๐Ÿง  Pareto Cheat Sheet

NeedFunction
Show somethingprint()
Random numbermath.random()
Remove decimalsmath.floor()
Check data typetype()
Text → Numbertonumber()
Number → Texttostring()

๐Ÿ† Boss Tip (The 80/20)

If I had to pick only three built-in functions for a beginner, I'd choose:

  1. print() → See what's happening.

  2. math.random() → Make games and randomness.

  3. tonumber() / tostring() → Convert between numbers and text.

Those alone solve a huge percentage of beginner programming tasks.


๐ŸŽ“ Congratulations!

At this point, you've learned the core 80% of Lua:

  • ✅ Variables

  • ✅ Decisions (if)

  • ✅ Loops

  • ✅ Functions

  • ✅ Tables

  • ✅ Loops + Tables

  • ✅ Strings

  • ✅ Essential built-in functions

From Chapter 9 onward, the topics become less about new fundamentals and more about organizing larger programs and understanding how professional Lua projects are structured. The concepts you've already learned are the foundation you'll use every day.

lua 7

Chapter 7 — Strings (๐Ÿ“ Working with Text)

If numbers are for math, strings are for words.

Almost every app uses strings.

  • User names

  • Passwords

  • Chat messages

  • Emails

  • Search bars

  • File names

  • URLs

If it looks like text, it's probably a string.


๐ŸŽฏ Mission

Imagine you're making WhatsApp.

A user sends:

Hello!

That's a string.


๐ŸŸข LEVEL 1 — Survival


Q1

Which of these is a string?

A)

100

B)

true

C)

"Hello"

D)

nil

Answer: C

๐Ÿ’ก Text inside quotes is a string.


Q2

Which is NOT a string?

A)

"Lua"

B)

"123"

C)

123

D)

"Hi"

Answer: C

๐Ÿ’ก No quotes = number.


Q3

name = "Pramod"

print(name)

Output?

A)

Pramod

B)

name

C)

"Pramod"

D) Error

Answer: A


Q4

Which is valid?

A)

city = Bangalore

B)

city = "Bangalore"

C)

city = Bangalore()

D)

city = {}

Answer: B


Q5

What are quotes (" ") mainly used for?

A) Numbers

B) Strings

C) Tables

D) Loops

Answer: B


Q6

print("Lua")

Output?

A)

Lua

B)

"Lua"

C) Error

D) nil

Answer: A


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

first = "Pramod"
last = "Tiwari"

print(first .. last)

Output?

A)

PramodTiwari

B)

Pramod Tiwari

C)

Pramod..Tiwari

D) Error

Answer: A

๐Ÿ’ก .. joins strings.


Q8

first = "Pramod"
last = "Tiwari"

print(first .. " " .. last)

Output?

A)

Pramod Tiwari

B)

PramodTiwari

C)

Pramod..Tiwari

D) Error

Answer: A


Q9

Which operator joins strings?

A)

+

B)

&

C)

..

D)

*

Answer: C

⚠️ Beginners often try +. Lua uses ...


Q10

print("Hello" .. " World")

Output?

A)

Hello World

B)

HelloWorld

C) Error

D)

World Hello

Answer: A


Q11

name = "Lua"

print(#name)

Output?

A) 2

B) 3

C) 4

D) Error

Answer: B

๐Ÿ’ก # gives the length.


Q12

print(#"Programming")

How many characters?

A) 9

B) 10

C) 11

D) 12

Answer: C


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

A website shows:

Welcome Pramod

Best code?

A)

print("Welcome")
print(name)

B)

print("Welcome " .. name)

C)

print(name + "Welcome")

D)

print(name)

Answer: B


Q14

You want:

Score: 100

Best approach?

A)

print("Score: " .. score)

B)

print(score + "Score")

C)

print(score)

D)

print("Score")

Answer: A


Q15

A game says:

Game Over

This is...

A) Number

B) String

C) Boolean

D) Table

Answer: B


Q16

You're building a login screen.

Username should be stored as...

A) Number

B) String

C) Boolean

D) Loop

Answer: B


Q17

A chatbot receives messages.

Each message is...

A) String

B) Table

C) Function

D) Number

Answer: A


Q18

A filename like:

photo.jpg

is usually stored as...

A) Number

B) String

C) Boolean

D) Loop

Answer: B


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

name = Pramod

Problem?

A) Missing quotes

B) Missing loop

C) Missing table

D) Nothing

Answer: A


Q20

print("Hello" + "World")

Problem?

A) Should use ..

B) Missing if

C) Missing end

D) Nothing

Answer: A


Q21

name = "Lua"

print(name + 1)

Problem?

A) Nothing

B) Can't add text and number with +

C) Missing quotes

D) Missing loop

Answer: B


Q22

word = "Cat"

print(#word)

Output?

A) 2

B) 3

C) 4

D) Error

Answer: B


Q23

print("Age: " .. 20)

Output?

A)

Age: 20

B) Error

C)

20Age

D)

Age:

Answer: A

๐Ÿ’ก Lua automatically converts the number when concatenating with ...


Q24

Which is correct?

A)

print("Hello" .. "Lua")

B)

print("Hello" + "Lua")

C)

print("Hello" * "Lua")

D)

print("Hello" & "Lua")

Answer: A


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

How do you join two strings in Lua?

A)

+

B)

..

C)

&

D)

=

Answer: B


Q26

How do you find the length of a string?

A)

length(name)

B)

#name

C)

size(name)

D)

count(name)

Answer: B


Q27

first = "Chat"
second = "GPT"

print(first .. second)

Output?

A)

Chat GPT

B)

ChatGPT

C)

GPTChat

D) Error

Answer: B


Q28

Which is most likely to be a string?

A) Health

B) Coins

C) Email Address

D) Player Level

Answer: C


Q29

Which operator is used most often with strings?

A)

+

B)

-

C)

..

D)

/

Answer: C


Q30 — Final Boss ๐Ÿ†

You're building a game.

You want to display:

Welcome Pramod!
Level: 25
Coins: 1000

What's your first thought?

A) Store everything as numbers.

B) Combine strings with variables using ...

C) Use only loops.

D) Use only tables.

Answer: B

๐Ÿ’ก Almost every app you use creates messages by joining text with variables. That's why string concatenation (..) is one of the most frequently used Lua features.


๐Ÿง  Pareto Cheat Sheet

SituationUse
Text"Hello"
Join strings..
Length of string#text
Store a name"Pramod"
Store an email"abc@gmail.com"
Display message"Welcome " .. name

๐Ÿ† The 80/20 Rule for Strings

If you remember only three things from this chapter, remember these:

  1. Text goes inside quotes"Hello"

  2. Join text with .."Hi " .. name

  3. Get length with ##name

Those three cover most of the string operations you'll use in beginner and intermediate Lua programs.

lua 6

Chapter 6 — Loops + Tables (๐Ÿค– The Warehouse Robot)

This chapter is where Lua becomes actually useful.

You've learned:

  • Variables → Remember one thing

  • Tables → Remember many things

  • Loops → Repeat work

Now combine them.

Think of it like this:

A table is a warehouse.

A loop is the robot walking through every shelf.


๐ŸŽฏ Mission

Imagine an inventory:

Sword
Shield
Potion
Bow
Apple

Without loops:

print(inventory[1])
print(inventory[2])
print(inventory[3])
print(inventory[4])
print(inventory[5])

๐Ÿ˜ฉ Boring.

Instead:

for i = 1,5 do
    print(inventory[i])
end

One loop.
Infinite happiness.


๐ŸŸข LEVEL 1 — Survival


Q1

You have 100 inventory items.

How should you print them?

A) 100 print() statements

B) One for loop

C) 100 variables

D) One if

Answer: B

๐Ÿ’ก Let the loop do the work.


Q2

fruits = {
    "Apple",
    "Banana",
    "Orange"
}

for i = 1,3 do
    print(fruits[i])
end

What prints first?

A) Banana

B) Apple

C) Orange

D) 1

Answer: B


Q3

Same code.

What prints last?

A) Apple

B) Banana

C) Orange

D) 3

Answer: C


Q4

Why is a loop useful with tables?

A) It visits every item automatically.

B) It creates more tables.

C) It prints only one item.

D) It deletes data.

Answer: A


Q5

numbers = {
    5,
    10,
    15
}

for i = 1,3 do
    print(numbers[i])
end

How many numbers are printed?

A) 1

B) 2

C) 3

D) Infinite

Answer: C


Q6

Which combination is most powerful?

A) Variable + Variable

B) Function + Function

C) Loop + Table

D) If + Boolean

Answer: C

๐Ÿ’ก This is used everywhere in real Lua projects.


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

items = {
    "Sword",
    "Bow",
    "Potion"
}

for i = 1,3 do
    print(items[i])
end

Output?

A)

Sword
Bow
Potion

B)

Potion
Bow
Sword

C)

Sword
Sword
Sword

D) Error

Answer: A


Q8

scores = {
    10,
    20,
    30
}

for i = 1,3 do
    scores[i] = scores[i] + 5
end

print(scores[2])

Output?

A) 20

B) 25

C) 30

D) 35

Answer: B

๐Ÿ’ก Every score increased by 5.


Q9

coins = {
    5,
    5,
    5
}

total = 0

for i = 1,3 do
    total = total + coins[i]
end

print(total)

Output?

A) 5

B) 10

C) 15

D) 20

Answer: C

๐Ÿ’ก This is called summing values.


Q10

animals = {
    "Dog",
    "Cat"
}

for i = 1,2 do
    print(i)
end

Output?

A)

1
2

B)

Dog
Cat

C)

Dog
1

D) Error

Answer: A

๐Ÿ’ก i is the counter, not the table item.


Q11

animals = {
    "Dog",
    "Cat"
}

for i = 1,2 do
    print(animals[i])
end

Output?

A)

Dog
Cat

B)

1
2

C) Error

D) nil

Answer: A


Q12

Which line accesses the current table item inside the loop?

A)

items

B)

items[i]

C)

i[items]

D)

items()

Answer: B


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

You have 200 player names.

Best approach?

A) 200 variables

B) Table + Loop

C) 200 functions

D) One if

Answer: B


Q14

A shop needs to display every product.

Best tool?

A) Loop through a table

B) 500 print statements

C) Boolean

D) Function only

Answer: A


Q15

Every enemy should lose 10 HP.

You have 50 enemies stored in a table.

Best approach?

A) Update each manually

B) Loop through the table

C) Use if

D) Use print

Answer: B


Q16

A teacher wants to print every student's name.

Best solution?

A) One table + loop

B) 100 variables

C) One function only

D) One boolean

Answer: A


Q17

Spotify wants to display your playlist.

What would they likely use?

A) Loop + Table

B) Variables only

C) if

D) Boolean

Answer: A


Q18

A game loads every saved item.

Best solution?

A) Table + Loop

B) Function only

C) Variable only

D) if

Answer: A


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

items = {
    "Sword",
    "Bow"
}

for i = 0,1 do
    print(items[i])
end

What's the problem?

A) Lua tables usually start at 1

B) Missing do

C) Missing end

D) Nothing

Answer: A


Q20

items = {
    "Sword",
    "Bow"
}

for i = 1,2 do
    print(items)
end

Problem?

A) Should be items[i]

B) Missing end

C) Missing for

D) Nothing

Answer: A


Q21

items = {
    "Sword",
    "Bow"
}

for i = 1,3 do
    print(items[i])
end

What prints last?

A)

Sword

B)

Bow

C)

nil

D) Error

Answer: C

๐Ÿ’ก There is no third item.


Q22

numbers = {
    10,
    20
}

for i = 1,2 do
    numbers[i] = numbers[i] * 2
end

print(numbers[2])

Output?

A) 20

B) 40

C) 10

D) 80

Answer: B


Q23

items = {
    "Sword"
}

print(items[5])

Output?

A) Error

B) Sword

C) nil

D) 5

Answer: C


Q24

What should the loop variable (i) usually represent?

A) The current position in the table

B) The whole table

C) A function

D) A boolean

Answer: A


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

Which pair is used in almost every Lua game?

A) Variable + Boolean

B) Table + Loop

C) If + Number

D) Function + Boolean

Answer: B


Q26

What does items[i] mean?

A) The entire table

B) The current item at position i

C) Create a table

D) Delete an item

Answer: B


Q27

scores = {
    10,
    20,
    30
}

for i = 1,3 do
    print(scores[i] + 1)
end

What prints?

A)

11
21
31

B)

10
20
30

C)

1
2
3

D) Error

Answer: A


Q28

If a table has 1,000 items, should you write 1,000 print() statements?

A) Yes

B) No, use a loop

C) Use 1,000 variables

D) Use 1,000 functions

Answer: B


Q29

You're building a game's inventory system.

Which idea is best?

A) One variable per item

B) Store items in a table and loop through them

C) Use only functions

D) Use only if

Answer: B


Q30 — Final Boss ๐Ÿ†

Complete the sentence:

Tables store the collection. Loops visit the collection.

Which answer matches that idea?

A) Tables and loops are unrelated.

B) Tables hold data, loops process each piece of data.

C) Loops replace tables.

D) Tables replace loops.

Answer: B

๐Ÿ’ก This is one of the most important patterns in programming. Whether you're making a game, a website, or an app, you'll constantly:

  1. Store many things in a collection (a table in Lua).

  2. Loop through that collection to display, update, search, or calculate something.


๐Ÿง  Pareto Cheat Sheet

SituationUse
Store many valuesTable {}
Visit every valuefor loop
Current itemtable[i]
Read an itemprint(table[i])
Change an itemtable[i] = ...
Missing itemnil

๐Ÿ† The Biggest Programming Pattern You've Learned

You now know the four building blocks that appear in almost every program:

  • ๐Ÿง  Variables → Remember one value.

  • ๐Ÿค” If → Make decisions.

  • ๐Ÿค– Loops → Repeat work.

  • ๐Ÿ“ฆ Tables → Store many values.

Almost every real program is just these four working together. Once you're comfortable combining them, the remaining chapters become much easier because they're mostly refinements of these core ideas.

lua 5

Chapter 5 — Tables (๐Ÿ”ฅ Lua's Superpower)

If you master only one advanced feature in Lua, make it Tables.

Almost everything in Lua—game inventories, players, enemies, settings, maps, menus, JSON data—is built with tables.


๐ŸŽฏ Mission

Imagine you own a backpack.

Without tables:

Sword
Shield
Potion
Apple
Bow
Arrow
Helmet
Gold

You'd need one variable for every item.

❌ Bad idea.

Instead:

inventory = {
    "Sword",
    "Shield",
    "Potion"
}

Now one variable (inventory) can hold many values.

Think of a table as a box with many compartments.


๐ŸŸข LEVEL 1 — Survival


Q1

What is a table mainly used for?

A) Storing many related values

B) Printing text

C) Repeating code

D) Making decisions

Answer: A

๐Ÿ’ก Tables let one variable hold lots of data.


Q2

Which creates a table?

A)

inventory = {}

B)

inventory = ()

C)

inventory = []

D)

inventory = <>

Answer: A

๐Ÿ’ก Lua uses curly braces {}.


Q3

inventory = {
    "Sword",
    "Shield",
    "Potion"
}

How many items are inside?

A) 1

B) 2

C) 3

D) 4

Answer: C


Q4

How do you access the first item?

A)

inventory[0]

B)

inventory[1]

C)

inventory.first

D)

inventory(1)

Answer: B

๐Ÿ’ก Lua starts counting from 1, not 0.

⚠️ This is one of the biggest beginner mistakes.


Q5

inventory = {
    "Sword",
    "Shield"
}

print(inventory[2])

Output?

A)

Sword

B)

Shield

C)

2

D)

nil

Answer: B


Q6

inventory = {}

print(inventory[1])

Output?

A)

0

B)

Empty

C)

nil

D)

1

Answer: C

๐Ÿ’ก No item exists there yet.


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

inventory = {
    "Sword",
    "Shield",
    "Potion"
}

print(inventory[3])

Output?

A)

Sword

B)

Shield

C)

Potion

D)

nil

Answer: C


Q8

inventory = {
    "Sword",
    "Shield"
}

inventory[2] = "Bow"

print(inventory[2])

Output?

A)

Shield

B)

Bow

C)

Sword

D)

nil

Answer: B

๐Ÿ’ก You replaced the second item.


Q9

numbers = {
    10,
    20,
    30
}

print(numbers[1] + numbers[3])

Output?

A) 20

B) 30

C) 40

D) 50

Answer: C

๐Ÿ’ก 10 + 30 = 40.


Q10

pets = {
    "Dog",
    "Cat"
}

pets[3] = "Rabbit"

print(pets[3])

Output?

A)

Rabbit

B)

Cat

C)

Dog

D)

nil

Answer: A


Q11

scores = {
    50,
    80,
    90
}

scores[1] = scores[1] + 10

print(scores[1])

Output?

A) 50

B) 60

C) 80

D) 90

Answer: B


Q12

Which statement is true?

A) Tables can only store numbers.

B) Tables can only store strings.

C) Tables can store different data types together.

D) Tables store only one value.

Answer: C

๐Ÿ’ก Tables are flexible.


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

A player has 10 weapons.

Best choice?

A) 10 variables

B) One table

C) 10 functions

D) One loop

Answer: B


Q14

A class has 50 student names.

Best choice?

A) 50 variables

B) One table

C) 50 functions

D) 50 loops

Answer: B


Q15

Which is better?

A)

weapon1 = "Sword"
weapon2 = "Bow"
weapon3 = "Axe"

B)

weapons = {
    "Sword",
    "Bow",
    "Axe"
}

Answer: B

๐Ÿ’ก Cleaner, shorter, easier to manage.


Q16

You're making a music playlist.

Best tool?

A) Boolean

B) Table

C) Function

D) if

Answer: B


Q17

A game inventory changes constantly.

Best tool?

A) Variable

B) Table

C) while

D) Boolean

Answer: B


Q18

A shopping cart stores many products.

Best choice?

A) Table

B) Function

C) Loop

D) if

Answer: A


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

inventory = []

What's wrong?

A) Lua doesn't use [] to create tables.

B) Missing if

C) Missing end

D) Nothing

Answer: A


Q20

inventory = {}

inventory[0] = "Sword"

print(inventory[1])

Output?

A)

Sword

B)

nil

C)

0

D) Error

Answer: B

๐Ÿ’ก Lua arrays normally start at 1, not 0.


Q21

inventory = {
    "Sword"
}

print(inventory[2])

Output?

A)

Sword

B)

2

C)

nil

D) Error

Answer: C


Q22

inventory = {}

inventory[1] = "Sword"

inventory[1] = "Bow"

print(inventory[1])

Output?

A)

Sword

B)

Bow

C)

Sword Bow

D)

nil

Answer: B

๐Ÿ’ก The old value gets replaced.


Q23

numbers = {
    5,
    10
}

print(numbers[1] * numbers[2])

Output?

A) 10

B) 15

C) 50

D) 5

Answer: C


Q24

Which is valid?

A)

player = {}

B)

player = []

C)

player = ()

D)

player = <>

Answer: A


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

What is the biggest advantage of tables?

A) Store many related values in one variable.

B) Replace loops.

C) Replace functions.

D) Replace if.

Answer: A


Q26

Lua tables usually start counting from...

A) 0

B) 1

C) -1

D) Depends on the computer

Answer: B

⚠️ This is the #1 thing to remember about Lua tables.


Q27

items = {
    "Apple",
    "Banana",
    "Orange"
}

print(items[2])

Output?

A)

Apple

B)

Banana

C)

Orange

D)

nil

Answer: B


Q28

Which real-life thing is most like a table?

A) A light switch

B) A backpack with many pockets

C) A calculator

D) A clock

Answer: B

๐Ÿ’ก One container holding many related things.


Q29

Which is easier to manage?

A)

coin1
coin2
coin3
coin4
coin5

B)

coins = {
    10,
    20,
    30,
    40,
    50
}

Answer: B


Q30 — Final Boss

You're making an RPG game.

A player has:

  • Name

  • Level

  • Health

  • Mana

  • Gold

  • Inventory

  • Quests

  • Skills

What's your first thought?

A) Make 8 separate variables.

B) Put everything inside a table.

C) Use only functions.

D) Use only loops.

Answer: B

๐Ÿ’ก In real Lua projects, a player is almost always represented as a table because it groups all related information together.


๐Ÿง  Pareto Cheat Sheet

SituationUse
Create a table{}
Get first itemtable[1]
Change an itemtable[2] = value
Empty table{}
Missing itemnil
Lua starts counting at1

๐Ÿ† Boss Tip (The Most Important Thing in This Chapter)

So far you've learned:

  • Variables → Store one value.

  • Functions → Group many actions.

  • Tables → Group many values.

That's a powerful mental model you'll keep using throughout Lua. In later chapters, you'll combine tables with loops and functions to build inventories, players, enemies, menus, and much more.

lua 4

Chapter 4 — Functions (๐Ÿฆธ Your Own Commands)

๐ŸŽฏ Mission

Imagine you're playing a game.

Every time the player wins, you want to show:

๐ŸŽ‰ You Win!
+100 XP
+50 Coins

Would you write those 3 lines 50 times throughout your program?

❌ No.

Instead, create your own command:

showReward()

That's exactly what a function is.

A function is your own custom command.


๐ŸŸข LEVEL 1 — Survival


Q1

What is the main purpose of a function?

A) Store data

B) Repeat code without copying it everywhere

C) Compare values

D) Create tables

Answer: B

๐Ÿ’ก Write once, use many times.


Q2

Which keyword creates a function?

A) func

B) method

C) function

D) create

Answer: C


Q3

function hello()
    print("Hello")
end

What did we just do?

A) Called the function

B) Created the function

C) Deleted the function

D) Printed Hello

Answer: B

๐Ÿ’ก The function exists now, but hasn't run yet.


Q4

function hello()
    print("Hello")
end

hello()

Output?

A)

Hello

B) Nothing

C) Error

D)

hello

Answer: A

๐Ÿ’ก hello() runs the function.


Q5

Which line calls a function?

A)

function hello()

B)

hello()

C)

end

D)

print = hello

Answer: B


Q6

Can you call the same function 100 times?

A) No

B) Yes

C) Only twice

D) Only inside loops

Answer: B

๐Ÿ’ก That's the whole point!


๐ŸŸก LEVEL 2 — Think Like a Programmer


Q7

function greet()
    print("Welcome!")
end

greet()
greet()

Output?

A)

Welcome!

B)

Welcome!
Welcome!

C) Nothing

D) Error

Answer: B


Q8

function jump()
    print("Jump!")
end

How many times does "Jump!" print?

A) 0

B) 1

C) 2

D) Infinite

Answer: A

๐Ÿ’ก The function is created but never called.


Q9

function attack()
    print("Hit!")
end

attack()
attack()
attack()

How many times is "Hit!" printed?

A) 1

B) 2

C) 3

D) 4

Answer: C


Q10

function bye()
    print("Bye")
end

print("Start")
bye()

What prints first?

A) Bye

B) Start

C) Error

D) Nothing

Answer: B


Q11

function test()
    print("Lua")
end

test()

print("Done")

What's printed last?

A) Lua

B) Done

C) test

D) Error

Answer: B


Q12

Functions help reduce...

A) Bugs caused by copying code

B) Variables

C) Loops

D) Tables

Answer: A

๐Ÿ’ก Copy-paste code is one of the biggest sources of bugs.


๐ŸŸ  LEVEL 3 — Real-Life Programming


Q13

Every time the player dies, you must:

  • Play sound

  • Show animation

  • Respawn player

Best approach?

A) Copy the code everywhere

B) Create playerDied()

C) Use a loop

D) Use if

Answer: B


Q14

A website sends an email after every purchase.

Best design?

A) Write email code every time

B) Create sendEmail()

C) Use while

D) Store it in a variable

Answer: B


Q15

You're making a calculator.

Which names make sense?

A)

abc()

B)

x()

C)

addNumbers()

D)

hello123()

Answer: C

๐Ÿ’ก Function names should describe what they do.


Q16

You want to play a button click sound everywhere.

Best approach?

A) Copy the sound code 50 times

B) Make playClickSound()

C) Use while

D) Use print

Answer: B


Q17

Which is easier to read?

A)

a()

B)

x()

C)

calculateTotalPrice()

D)

zzz()

Answer: C


Q18

You want to reuse the same logic in many places.

What's the best tool?

A) Variable

B) Function

C) Table

D) Boolean

Answer: B


๐Ÿ”ด LEVEL 4 — Bug Hunter


Q19

function hello()
    print("Hi")

What's wrong?

A) Missing end

B) Missing if

C) Missing for

D) Nothing

Answer: A


Q20

function hello()
    print("Hi")
end

Hello()

Problem?

A) Nothing

B) Lua ignores uppercase/lowercase

C) Function name doesn't match

D) Missing print

Answer: C

๐Ÿ’ก hello and Hello are different names.


Q21

function run()
    print("Running")
end

Why doesn't anything print?

A) Missing run()

B) Missing end

C) Missing while

D) Missing for

Answer: A


Q22

function score()
    print("100")
end

score

Problem?

A) Nothing

B) Forgot ()

C) Missing end

D) Missing if

Answer: B

๐Ÿ’ก score refers to the function; score() runs it.


Q23

function hello()
    print("Hi")
end

hello()
hello()

How many times does it run?

A) 1

B) 2

C) 3

D) Infinite

Answer: B


Q24

What's better?

A)

function a()

B)

function doStuff()

C)

function updatePlayerHealth()

D)

function xyz()

Answer: C

๐Ÿ’ก Clear names make code easier to understand months later.


๐ŸŸฃ LEVEL 5 — Boss Battle ๐Ÿ‘‘


Q25

A function is best described as...

A) Memory

B) A custom command

C) A loop

D) A table

Answer: B


Q26

Creating a function automatically runs it.

A) True

B) False

Answer: B

๐Ÿ’ก You must call it with ().


Q27

What does this print?

function dance()
    print("๐Ÿ’ƒ")
end

dance()
dance()
dance()

A)

๐Ÿ’ƒ

B)

๐Ÿ’ƒ๐Ÿ’ƒ

C)

๐Ÿ’ƒ
๐Ÿ’ƒ
๐Ÿ’ƒ

D) Nothing

Answer: C


Q28

Which is the biggest advantage of functions?

A) Faster internet

B) Reuse code

C) Create tables

D) Replace variables

Answer: B


Q29

Which is the most professional function name?

A)

a()

B)

test123()

C)

calculateDamage()

D)

x()

Answer: C


Q30 — Final Boss

If you find yourself copying the same 10 lines of code into 8 different places, what should your first thought be?

A) Keep copying

B) Make the code longer

C) Put those 10 lines inside a function

D) Create more variables

Answer: C

๐Ÿ’ก One of the biggest habits of good programmers is this:

If you're copying code, stop and ask: "Should this be a function?"


๐Ÿง  Pareto Cheat Sheet

SituationUse
Create a functionfunction name()
End a functionend
Run a functionname()
Use functions forReusing code
Good function namescalculateScore(), playSound(), spawnEnemy()
Avoida(), x(), test() (unless it's truly a test)

๐Ÿ† Boss Tip

From now on, think of functions like the built-in commands you've already used:

  • print() → Built-in function.

  • math.random() → Built-in function.

  • tonumber() → Built-in function.

Now you're creating your own commands:

  • attackEnemy()

  • saveGame()

  • healPlayer()

  • openDoor()

That's a major step—from just using a programming language to extending it with your own reusable building blocks.