Games

Lua: Script your game

Roblox-style Lua: from your first print to coins, lava and a working obby.

9 chapters · 38 lessons · free

Try a free lesson Create your free account

What you will learn

1. Hello, Lua

Lua is the language of Roblox scripts. Print your first messages and learn how comments work.

  • Your first script Try it print("text") shows a message in the Output. It is the first tool for seeing what a script does.
  • A game intro Exercise Scripts run from top to bottom. The order of the lines is the order things happen.
  • Notes for humans Exercise -- starts a comment. Everything after it on that line is ignored.
  • The comment that broke the script Debug One unreadable line stops the whole script. Lua comments are --, not //.
  • Quick check Quiz print for messages, -- for comments, and the script runs top to bottom.

2. Variables

Coins, health, player names: games are full of values that change. Store them in variables, do math, and join text with ..

  • Store a value Try it local name = value creates a variable. .. joins text and values.
  • Taking damage Exercise x = x - 5 (or x -= 5) changes a variable using its own old value. Math uses + - * /.
  • Adding text with + Debug + adds numbers. .. joins text. Mixing them up is the classic Lua mistake.
  • Is the player alive? Exercise Lua types: number, string, boolean, nil (and table, coming soon). type(x) names them.
  • Quick check Quiz local variables, math that updates them, .. for text, and the types number, string, boolean and nil.

3. Decisions with if

Can the player afford the sword? Is the boss defeated? if, elseif and else let your game decide.

  • Can I buy the sword? Exercise if / then / else / end. Comparisons: ==, ~= (not equal), <, >, <=, >=.
  • Health colors Exercise if / elseif / else: the first true branch runs, the rest are skipped.
  • The missing word Debug if ... then ... end: both then and end are required.
  • Two keys for one door Exercise and, or, not combine conditions.
  • Quick check Quiz if/then/elseif/else/end with comparisons and and/or/not.

4. Loops

Countdowns, waves of enemies, laps: loops repeat code so you don't have to.

  • Count the laps Exercise for i = start, stop do ... end repeats, and i tells you which round it is.
  • Countdown Exercise The third number of a for loop is the step. -1 counts down.
  • Spawn until full Exercise while condition do ... end repeats until the condition is false.
  • The loop that never stops Debug A while loop needs something inside that eventually makes the condition false.
  • Quick check Quiz for with a counter, a step for counting down, and while for "until something happens".

5. Tables and functions

An inventory is a table, a player is a table, and a heal is a function. The two building blocks of every Roblox game.

  • The inventory Exercise Tables as lists: {a, b}, table.insert, #list, and for i, v in ipairs(list).
  • A player as a table Exercise Tables with names: { Key = value }, read and change with table.Key.
  • A heal function Exercise local function name(params) ... return value end packs code into a reusable piece.
  • name or Name? Debug Name and name are different keys. A missing key gives nil.
  • Quick check Quiz Tables hold lists and named values. Functions are reusable code that can return results.

6. Build the world

Parts are the blocks every Roblox world is made of. Create them, size them, color them and place them, from code.

  • Your first part Try it Instance.new("Part"), then set Name, Size, Position, Color, Anchored, and Parent = workspace.
  • A staircase Exercise Use the loop counter in Position to place parts in a pattern.
  • A glass window Exercise Transparency, Shape and Material change how a part looks.
  • The invisible platform Debug Nothing appears until its Parent is set (usually to workspace). Set Parent last.
  • Quick check Quiz Instance.new, Vector3, Color3, Transparency, Shape, and Parent = workspace.

7. Players and events

Games react: a player joins, a coin is touched, someone steps on lava. Events and :Connect make it happen.

  • Welcome, player Exercise event:Connect(function(args) ... end) runs your function every time the event fires.
  • A leaderboard Exercise A Folder named leaderstats under the player, with IntValues inside, makes a leaderboard.
  • Collect the coin Exercise Touched + GetPlayerFromCharacter + leaderstats: the core of collecting things in Roblox.
  • The lava block Exercise FindFirstChild safely looks for a child: it returns nil instead of crashing when it is not there.
  • The spikes that crashed Debug hit is a body part; the Humanoid is in hit.Parent. FindFirstChild avoids crashes.
  • Quick check Quiz PlayerAdded, Touched, :Connect, leaderstats and Humanoid: real Roblox game scripting.

8. Lua exam

Ten questions about Lua and Roblox scripting. Pass with 8 right answers.

  • The Lua exam Exam Variables, decisions, loops, tables, functions, parts and events: you can script real Roblox games.

9. Final project

Script a mini obby of your own: a course, collectibles, danger and a leaderboard. Then send it for review.

  • Your own obby Final project Players, leaderstats, parts and events together: that is a real Roblox game.

How the course works

You learn by doing: each lesson explains one idea, then you write real code in the editor and see the result right away. The first lesson is free to try without an account; sign up to save your progress and continue.

  • Try it

    Change working code and see the result immediately.

  • Exercise

    Complete a task. A checklist confirms each requirement as you type.

  • Debug

    Fix code that is broken on purpose, and learn why it failed.

  • Quiz

    Check your understanding at the end of a chapter.