Reading errors, docs and getting help (including AI as a tutor)

Learn to read Luau errors and stack traces, search the docs and DevForum, build a minimal reproduction, and use AI assistants as a tutor.

Concept lesson, about 45 minutes, 16 steps

By the end you can

  • Read a Luau error and a stack trace to find the script, the line and the cause.
  • Search the Roblox docs and the DevForum, and cut a problem down to a minimal reproduction.
  • Ask a question that gets answered, and use an AI assistant as a tutor without letting it think for you.

Before this: Programming from zero, part 3: tables and the coin counter

1Red text is a gift

Sooner than you think, code you wrote will stop and print red text. It is tempting to read that as "you failed". Read it as "here is exactly what went wrong and where". Professional developers do not make fewer errors than beginners. They read them faster.

Run this cell. It has a bug on purpose, a small test of awarding coins:

Luau
local function award(coins, pickups)
	return coins + pickups * 5
end
local function onPadTouched(playerName)
	return award(10)
end
onPadTouched("Player1")
Figure 1.1. Run it in the lesson view or in the Studio command bar.

The runner stops and shows one error line under the cell. By the end of this lesson every word of it will make sense. Real tools print more than the browser runner does, and you will meet them in the setup lessons. They add a trace under the line, and the trace gets its own step below.

Red text appears when you run your code. What is the best first move?

  1. Undo everything you did today
  2. Read the first red line: which script, which line, what happened
  3. Close the page and reopen it
  4. Ask someone to fix it without reading it
Answer

Read the first red line: which script, which line, what happened. The first error line names the script, the line number and the problem. That is usually enough to find it.

Open this step in lesson mode · Link to this step · This confused me

2Who, where, what

Both sidesAn error message is the panel by a building's front door, which names the room and the zone and lists the doors the smoke came through.
In the pictureIn Roblox
Which roomThe script name or path
The zone number on the panelThe line number
What the detector sensedThe error message
The doors the smoke came through, nearest firstThe stack trace

See the whole analogy cast

Think of the panel by a building's front door. When a smoke detector trips, the panel does not just ring. It shows three things, and an error line shows the same three.

Diagram
Anatomy of an error line. The line ServerScriptService.ErrorDemo:2: attempt to perform arithmetic (mul) on nil and number, split into the script path, the line number and the message.
Figure 2.1.

Every Luau error line has the same three parts.

  • Who: the script's name. In a real game it is a full path, such as ServerScriptService.ErrorDemo. In the browser runner your code is called main, so you saw main:2:.
  • Where: the line number, between colons.
  • What: the message. It names the operation that failed (arithmetic (mul), so a *) and the kinds of value involved (nil and number).

Think of it as The panel by a building's front door, which names the room and the zone and lists the doors the smoke came through

Take: ServerScriptService.Server.CoinService:48: attempt to index nil with 'Coins'. Match each part.

  1. ServerScriptService.Server.CoinService
  2. 48
  3. attempt to index nil
  4. 'Coins'
Answer

ServerScriptService.Server.CoinService: Which script; 48: Which line; attempt to index nil: What it tried: reading a field from nothing; 'Coins': The field it tried to read. 'Index' means reading a field with a dot or brackets. Something on line 48 was nil, and the code tried to read .Coins from it.

Open this step in lesson mode · Link to this step · This confused me

3Five messages you will see a lot

Most beginner errors are one of a handful of messages. Each cell below triggers one on purpose; press Run and match it to the table.

MessageWhat it meansUsual cause
attempt to index nil with 'X'You read .X from something that is nila misspelt name, or an object that does not exist yet
attempt to perform arithmetic (add) on nil and numberYou did maths with nila variable or argument never set
attempt to call a nil valueYou called something that is not a functiona misspelt function name
attempt to compare number < nilYou compared a number with nila missing value in an if
Expected 'end' (to close 'function' at line 1)The code does not parse at alla missing end
Luau
local profiles = { Player1 = { coins = 5 } }
print(profiles.Player1.coins)
print(profiles.Player2.coins)
Figure 3.1. Run it in the lesson view or in the Studio command bar.

What error does this produce?

local Shop = {}
function Shop.buy(name)
	return true
end
Shop.Buy("Player1")
  1. main:5: attempt to call a nil value
  2. main:2: attempt to index nil with 'buy'
  3. main:5: Expected 'end'
  4. It prints true
Answer

main:5: attempt to call a nil value. Shop.Buy with a capital B does not exist, so it is nil, and calling nil fails on line 5. Names are case-sensitive.

Open this step in lesson mode · Link to this step · This confused me

4Read the message literally

Messages are short and exact, so read every word. This one surprises people:

Luau
local coins = nil
if coins > 10 then
	print("rich")
end
Figure 4.1. Run it in the lesson view or in the Studio command bar.

The code says coins > 10, but the message says number < nil. Luau turned a > b into b < a before running it, so the sides swapped. The message is still telling the truth: one side was a number and the other was nil. Find the nil.

What error does this produce?

local playerName
print("Welcome, " .. playerName)
  1. main:2: attempt to concatenate string with nil
  2. main:1: attempt to index nil
  3. main:2: attempt to call a nil value
  4. It prints Welcome, nil
Answer

main:2: attempt to concatenate string with nil. local playerName with no value is nil, and .. cannot join text with nil. tostring(playerName) would print 'nil' instead of failing.

Open this step in lesson mode · Link to this step · This confused me

5The stack trace: the doors the smoke came through

The first line says where the error happened. The lines that real tools print under it say how the program got there. Each function call waits on the one it called, like a stack of plates. The top line is the function that failed, and each line below it is the one that called the line above.

Both sidesRemember: An error message is the panel by a building's front door, which names the room and the zone and lists the doors the smoke came through.

Roblox Studio, which you install in the setup lessons, prints this trace for the cell you ran at the top of the lesson. Its first line has the same three parts as before:

text
ServerScriptService.ErrorDemo:2: attempt to perform arithmetic (mul) on nil and number
Stack Begin
Script 'ServerScriptService.ErrorDemo', Line 2 - function award
Script 'ServerScriptService.ErrorDemo', Line 5 - function onPadTouched
Script 'ServerScriptService.ErrorDemo', Line 7
Stack End

Read it from the bottom up to tell the story in order: line 7 called onPadTouched, which on line 5 called award, which failed on line 2. In Set up your machine, part 3: connect, play, and the optional extras you will make Studio print a trace like this for real.

Put the events in the order they happened.

  1. Line 7 calls onPadTouched("Player1")
  2. Line 5 calls award(10), with no second argument
  3. Luau raises the error and the trace is printed
  4. Line 2 multiplies pickups, which is nil, by 5
Answer

1. Line 7 calls onPadTouched("Player1") 2. Line 5 calls award(10), with no second argument 3. Line 2 multiplies pickups, which is nil, by 5 4. Luau raises the error and the trace is printed. The trace lists these newest first; reading it bottom-up gives the order they happened.

Open this step in lesson mode · Link to this step · This confused me

6Where the alarm picture breaks

A smoke detector sounds close to the fire. Errors do not. Luau stops where a bad value is used, which is often far from where it was made.

In the trace above, the error is on line 2, inside award. But line 2 is correct code. The mistake is on line 5, where award(10) forgot its second argument. Line 2 is just where the missing value was finally noticed.

Where does it break?“The panel by a building's front door, which names the room and the zone and lists the doors the smoke came through” is a good picture of an error message. Where does it stop being true?

It breaks here. A smoke detector sounds close to the fire, but an error sounds at the line that used a bad value, which can be far from the line that made it, and the trace lists only the calls still running.

The true model. The virtual machine raises an error at the instruction that fails, and the stack trace lists the active calls at that moment, innermost first. The cause can be any earlier code that produced the bad value.

Which line holds the cause of the nil in the ErrorDemo script, the place where the missing value should have been supplied?

  1. Line 2, the line the error names
  2. Line 5: award(10) leaves out the second argument
  3. Line 7: onPadTouched is called at all
  4. Line 1: the function declares a pickups parameter
Answer

Line 5: award(10) leaves out the second argument. Line 2 only noticed the nil. The nil came from line 5, which left out an argument. Walk down the trace until you reach the line where the bad value was created. A guard on line 2 can still be a sensible extra, but it is a patch, not the cause.

Open this step in lesson mode · Link to this step · This confused me

8Search the docs first

Good place for a break. Your answers and progress are saved in this browser, so you can stop here and come back to this step. Tick what you can do now; if something is shaky, go back one step first.

Checkpoint

  • I can name the three parts of an error line: which script, which line and what happened
  • I can read a stack trace from the bottom up
  • I know to print values and their types to find where a nil came from

The official documentation answers most "how does X work?" questions faster than any person can.

  • Roblox Creator Hub docs, at create.roblox.com/docs, cover Studio, guides and tutorials 1.
  • The Engine API reference has one page per class, such as Players or Part, listing every property, method and event 2. If an API is not there, it does not exist.
  • luau.org documents the language itself: syntax, types and the standard library 3.

Search with the class and member name together, such as Players.PlayerAdded or TweenService:Create. Read the page's code sample before anything else; it is usually the fastest answer.

Web browser: create.roblox.com/docs
The Roblox Creator Hub docs search box with 'Players.PlayerAdded' typed and the PlayerAdded event result highlighted

Select a numbered pin, or a row below, to read what it marks.

Figure 8.1.

Search with the class and member name together.

You want to know what arguments Players.PlayerAdded passes to your function. Where do you look first?

  1. A YouTube video from 2019
  2. The Players class page in the Engine API reference
  3. A random Discord server
  4. Guess and press Play
Answer

The Players class page in the Engine API reference. The API reference is official and current, and each event lists its parameters. Old videos often show APIs that have since changed.

Open this step in lesson mode · Link to this step · This confused me

9The DevForum

The Developer ForumDeveloper Forum (DevForum)Roblox's official forum for developers, where staff announce changes and people ask for help.Think of it as the notice board in the middle of the yard, read by everyone who builds. at devforum.roblox.com is where Roblox developers, and Roblox staff, discuss problems and announce changes 4. Search it before you post. Someone has very likely hit the same error and posted the answer.

If you do post, the Scripting Support category asks every topic to answer three questions: what you want to achieve, what the issue is (with screenshots or video if possible), and what you have already tried, including whether you checked the Creator Hub 5. Its rules also say to paste code as text between triple backticks, never as a screenshot, and not to ask people to write whole scripts or systems for you.

Web browser: devforum.roblox.com
DevForum search results for 'attempt to index nil with', filtered to the Scripting Support category, with solved topics marked

Select a numbered pin, or a row below, to read what it marks.

Figure 9.1.

Search the exact error message, filtered to Scripting Support.

Which follow the Scripting Support rules?

  1. Saying what you want to achieve, what goes wrong and what you tried
  2. Pasting your code as text in triple backticks
  3. Posting a phone photo of your screen with the code on it
  4. Asking someone to write your whole round system
  5. Searching the forum before posting
Answer

Saying what you want to achieve, what goes wrong and what you tried; Pasting your code as text in triple backticks; Searching the forum before posting. Text can be read, searched and copied. Asking for a whole system belongs in a hiring category, not support.

Open this step in lesson mode · Link to this step · This confused me

10Shrink it: the minimal reproduction

The most powerful debugging skill has a name: the minimal reproductionMinimal reproductionThe smallest piece of code that still shows a bug.Think of it as the bug in a jar, with everything else removed.. Make a copy, then delete everything that is not needed to make the bug happen, until what is left is as small as it can be and still shows the problem 6.

Two things happen when you do this. Half the time you find the bug yourself while cutting, because the thing you just removed made it go away. The other half, you end up with ten lines that anyone can understand, instead of your whole game.

Here is a bug from a shop script, cut down from 80 lines to the few that matter. It runs in the browser, so anyone can try it:

Luau
-- Minimal reproduction: buying the Shield always fails, even with 50 coins.
local PRICES = { Speed = 25, Shield = 40 }
local coins = 50
local item = "shield"
 
print(PRICES[item])
print(coins >= PRICES[item])
Figure 10.1. Run it in the lesson view or in the Studio command bar.

The reproduction made the cause obvious. Fix it so the last line prints true.

local PRICES = { Speed = 25, Shield = 40 }
local coins = 50
local item = "shield"
print(coins >= PRICES[item])
Answer

Line 3: local item = "shield". Fixed: local item = "Shield". Keys are case-sensitive: "shield" is not "Shield", so the lookup returned nil. Printing PRICES[item] on its own line showed that at once.

Open this step in lesson mode · Link to this step · This confused me

11Ask a question that gets answered

When you do ask, in a "this confused me" issue, on the DevForum or to a friend, give the helper everything they need in one message:

Checkpoint

  • Goal: what I am trying to do, in one sentence
  • Expected and actual: what I expected, and what happened instead
  • The exact error, copied as text, including the stack trace
  • A minimal reproduction, as text in a code block
  • What I tried, and what each attempt did
  • My setup: Windows or macOS, and where I ran the code (the browser runner or Studio)

Leave out passwords, cookies, API keys and your real name. Nobody needs them to help with code 7.

Which question is most likely to get a fast, correct answer?

  1. 'my game is broken pls help'
  2. 'Shop doesn't work. Here is my whole place file.'
  3. 'Buying Shield fails with 50 coins. Expected true, got: main:7: attempt to compare nil <= number. 6-line repro below. I checked the price table and it has Shield = 40.'
  4. 'Can someone add a shop to my game?'
Answer

'Buying Shield fails with 50 coins. Expected true, got: main:7: attempt to compare nil <= number. 6-line repro below. I checked the price table and it has Shield = 40.'. It has the goal, expected and actual, the exact error, a small reproduction and what was checked. A helper can answer it in a minute.

Open this step in lesson mode · Link to this step · This confused me

12AI assistants as a tutor

AI assistants are now part of normal development. Roblox Studio has its own Assistant, which answers questions, explains selected code and can create and change objects and scripts in your place 8. Editors such as Cursor have one built in, and general chat assistants answer Luau questions too.

Used well, an AI is a patient tutor available at 3 a.m. Used badly, it writes code you do not understand, and you learn nothing. Rules for this course:

  • Ask it to explain, not to fix. "What does attempt to index nil with 'coins' mean, and what should I check?" teaches you. "Fix my script" does not.
  • Ask for a hint first. Try the hint before you ask for the answer.
  • Check every API name it uses against the Engine API reference. AIs invent plausible methods that do not exist.
  • Never paste secrets: cookies, .ROBLOSECURITY, API keys or passwords.
  • You must be able to explain every line you keep. If you cannot, you are not finished.

Which prompt uses an AI as a tutor?

  1. 'Write the whole coin system for my game.'
  2. 'Here is my error and the 8 lines around it. Explain what the message means and give me one hint, not the fix.'
  3. 'Fix this.' plus a pasted 400-line script
  4. 'Here is my .ROBLOSECURITY cookie, check my account.'
Answer

'Here is my error and the 8 lines around it. Explain what the message means and give me one hint, not the fix.'. It asks for understanding and a hint, with just enough context. The cookie prompt is never acceptable: it hands over your account.

Open this step in lesson mode · Link to this step · This confused me

13Which assistant, for what

AI assistants you will meet

OptionWhat it isWhen pros pick itTrade-offs
Studio AssistantAn assistant built into Studio that can see your place and act on it, so it can insert objects and edit scripts for you.Quick experiments inside a place, with a person reviewing every change.It changes things. Review its edits the way you would review a teammate's, and commit before asking for anything large.
An AI in your editor (Cursor and others)An assistant that sees the files in your project folder, so it can explain code in context and point at the line it means.Explaining unfamiliar code in a project you already understand a little.It can change many files at once. Read its diff before you accept anything.
A general chat assistantA chat window that sees only what you paste.Explaining an error message or a minimal reproduction, as a tutor.A poor judge of your whole game. Paste small snippets, never secrets.

Sources: 8

You want an explanation of one confusing error message without giving anything access to your project. Which fits best?

  1. Studio Assistant with permission to edit your place
  2. A general chat assistant, with the error text and a minimal reproduction pasted in
  3. An editor AI told to fix every file
  4. None; AI should never be used
Answer

A general chat assistant, with the error text and a minimal reproduction pasted in. A chat assistant sees only what you paste, which is all it needs to explain a message. Tools that can edit your project are useful too, but review what they change.

Open this step in lesson mode · Link to this step · This confused me

14Check what the AI tells you

An assistant suggests this to award coins:

luau
local function onTouched(player)
	player:AddCoins(Config.COIN_VALUE)
end

It looks confident and tidy. Before using it, look up Player in the Engine API reference. There is no AddCoins method on Player. The AI invented it because the name sounds right. In Beacon Yard, coins live in the server's own data, which the coin and data lessons build, not on the Player object.

This is the habit that keeps AI useful: trust, then verify, against the docs, against a quick test, or against the error you get when you run it.

An AI's code calls player:AddCoins(5). What do you do?

  1. Use it; AI is usually right
  2. Search the Player page in the Engine API reference, find no AddCoins, and ask the AI where that method comes from or how coins are really stored
  3. Rename your own function to AddCoins
  4. Post it on the DevForum as a bug
Answer

Search the Player page in the Engine API reference, find no AddCoins, and ask the AI where that method comes from or how coins are really stored. The API reference is the source of truth. A method that is not listed there does not exist, however confident the suggestion sounded.

Open this step in lesson mode · Link to this step · This confused me

15Challenges

Level 2, Builder

Challenge

This function fails. Read the error, find the cause and fix it so every test passes.

Start from
The code cell below the challenge, which runs a hidden test when you press Run.
Check it here
Run the code cell in the lesson view; the challenge completes when it passes. No Studio needed.

Done means

  • average({10, 20, 30}) is 20
  • average({}) is 0, not nan
  • Self-check, not graded: you can say which line the error named and which line held the cause

Hints

  1. The error says: attempt to get length of a nil value. Which name on that line is nil?
  2. Names are case-sensitive. After that, what does 0 / 0 give for an empty list?
Solution
luau
local function average(list)
	if #list == 0 then
		return 0
	end
	local total = 0
	for _, n in list do
		total += n
	end
	return total / #list
end
Luau
-- average should return the mean of a list of numbers, or 0 for an empty list.
-- Run it: it fails. Read the error, find the cause, fix it.
local function average(list)
	local total = 0
	for _, n in list do
		total += n
	end
	return total / #List
end
Figure 15.1. Run it in the lesson view or in the Studio command bar. The lesson view checks it with tests.

Level 1, Apprentice

Challenge

Write one good question about a real bug, in the format helpers want to read.

Start from
A bug you hit in any lesson so far, or the award snippet from this lesson.

Done means

  • You wrote a question with goal, expected, actual, the exact error text and what you tried.
  • It includes a minimal reproduction of 15 lines or fewer, as text.
  • It contains no personal details, passwords, cookies or keys.

Hints

  1. Cut the code down until removing anything else makes the bug disappear.

Open this step in lesson mode · Link to this step · This confused me

16Recap

Recap

  1. An error line says who (the script path), where (the line) and what (the rule broken). The stack trace lists the calls that led there, newest first; read it bottom-up for the story.
  2. Errors fire where a bad value is used, not where it was made. Print the values and their types, then shrink the problem to a minimal reproduction.
  3. Search the API reference, luau.org and the DevForum first. Ask with goal, expected, actual, exact error and repro. Use AI to explain and hint, verify every API it names, and never paste secrets.

An error message is the panel by a building's front door, which names the room and the zone and lists the doors the smoke came through, until a smoke detector sounds close to the fire, but an error sounds at the line that used a bad value, which can be far from the line that made it, and the trace lists only the calls still running.

Next How to use this course

Open this step in lesson mode · Link to this step · This confused me