← Back to All Parts
📖
Part 38 min

Understanding Lua (Roblox's Language)

Before we build more cool stuff, let's learn some Lua basics. Think of this like learning the ABCs before writing stories!

Variables - Giving Names to Things

A variable is like a labeled box where you store stuff.

-- Creating variables
local playerName = "Alex"     -- A box holding text
local playerHealth = 100      -- A box holding a number
local isAlive = true          -- A box holding true/false

-- Using variables
print(playerName)   -- Shows: Alex
print(playerHealth) -- Shows: 100

If Statements - Making Decisions

These let your code make choices, like "if this, then do that"

-- Check if player has enough health
local health = 50

if health > 0 then
    print("Player is alive!")
else
    print("Game over!")
end

Loops - Doing Things Over and Over

Loops repeat code. Perfect for countdowns!

-- Count from 1 to 5
for i = 1, 5 do
    print(i)
    wait(1)  -- Wait 1 second
end
print("Blast off!")

Functions - Reusable Code Recipes

A function is like a recipe you can use over and over. Functions are like Scratch's "My Blocks"! You make them once and use them many times.

-- Create a function
local function sayHello(name)
    print("Hello, " .. name .. "!")
end

-- Use the function
sayHello("Emma")  -- Shows: Hello, Emma!
sayHello("Jake")  -- Shows: Hello, Jake!

Try It Yourself: Countdown Script

Make a new Script and try this countdown:

-- Countdown script
for i = 10, 1, -1 do
    print(i .. " seconds left!")
    wait(1)
end
print("TIME'S UP!")

✅ Checklist - Check them off!

You learned the building blocks of all Roblox code!