Menu
CoddyTech

Why Use Luau?

Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 2 of 80.

Luau's type system offers three major benefits that make development safer and more efficient.

Catch errors early: the type checker analyzes your code while you edit, before it runs. Consider a classic Lua bug: passing nil where a number is expected:

-- Lua: nothing warns you about this
local function calculateTotal(price, tax)
    return price + tax
end

print(calculateTotal(nil, 0.1))
-- ✗ runtime error: attempt to perform arithmetic on nil

In plain Lua that crash only happens when the bad call finally executes: maybe in front of your users. Worse, Lua sometimes hides the bug entirely: "50" + 0.1 silently coerces the string and returns 50.1… until the day the string isn't numeric. With Luau annotations the mistake is flagged immediately:

local function calculateTotal(price: number, tax: number)
    return price + tax
end

calculateTotal(nil, 0.1) -- ✗ type error while editing: nil is not a number

Better code readability: annotations serve as documentation. price: number tells you and your teammates exactly what a function expects: no guessing, no digging through the implementation.

Enhanced development tools: because the editor knows each variable's type, you get accurate autocompletion, safer refactors, and smarter navigation. Type a dot after a typed value and the editor lists exactly what's available.

challenge icon

Challenge

Easy

Give both parameters of calculateTotal the type number.

  1. In the first line, change (price, tax) to (price: number, tax: number).
  2. Keep the call calculateTotal(80, 8) at the bottom.

Press Run Code: the output shows 88.

Try it yourself

local function calculateTotal(price, tax)
    return price + tax
end

print(calculateTotal(80, 8))

All lessons in Introduction To Luau

Practice on your own: Online Luau compiler