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.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.