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
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 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!") 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! 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!")