attempt to compare nil < numberin Roblox Luau: what it means and how to fix it

What it means

You used <, >, <= or >= with nil on one side. Luau can only order two numbers or two strings, so the comparison fails. The message always shows < or <=, because > and >= are checked with the two sides swapped.

The same error also shows up as:

  • attempt to compare number < nil
  • attempt to compare nil <= number

The smallest script that triggers it

Triggers the errorline 2: error here
local stats = {}
if stats.level < 5 then
  print("Beginner")
end

The fix

Fixedline 1: the fix
local stats = { level = 1 }
if stats.level < 5 then
  print("Beginner")
end

Reading a field that was never set gives nil. Setting level first means both sides of < are numbers.

Common causes

  1. A table field was never set, or its name is misspelled.
  2. tonumber returned nil because the text was not a number, for example an empty TextBox.
  3. A value loaded from a data store was nil for a new player.

Practice this until it sticks

A Deep Dive on comparisons and nil: 5-minute lessons, each with a quiz. What you miss comes back in the next module. One Deep Dive a day is free. No card.