Roblox Studio error

invalid argument #2 to 'random' (interval is empty)

What invalid argument #2 to 'random' (interval is empty) means in Roblox Studio, the three things that cause it with the code that triggers each, and the fix for each one.

Updated 14 November 20263 min readErrorLuauWritten by

In short

math.random(m, n) picks a whole number from m to n, and math.random(m) picks from 1 to m. interval is empty means there was no number to pick: n was smaller than m, or m was 0. Most of the time the cause is math.random(#list) on an empty list. Check that the list has items before you pick one.

  • #2 means the second number is below the first, as in math.random(10, 1).
  • #1 means a single number below 1, usually math.random(#list) with an empty list.
  • Check #list > 0 first, or order the two numbers with math.min and math.max.
On this page
  1. What it means
  2. Causes and fixes
  3. Fix it with RoCode
  4. Find the line
  5. Questions
  6. Sources

What the message means

Output (example)
ServerScriptService.Loot:8: invalid argument #2 to 'random' (interval is empty)

Other forms of the same message:

  • invalid argument #1 to 'random' (interval is empty)

A range with no numbers in it is "empty". math.random(5, 1) asks for a number at least 5 and at most 1, which does not exist, so Luau stops the script instead of guessing.

The argument number tells you which call shape failed. #1 comes from the one-number form, math.random(m), when m is below 1. #2 comes from the two-number form when the second number is smaller than the first.

Causes and fixes

Three things cause this error in practice. Each one below has the smallest code that triggers it, then the fix.

Picking from a list that is empty

Picking a random item is usually written list[math.random(#list)]. If the folder has no children yet, or every item was already removed, #list is 0 and the call fails with #1.

Breaks Script in ServerScriptService named Loot
local spawnFolder = workspace:WaitForChild("LootSpawns")

local function pickSpawn()
	local spawns = spawnFolder:GetChildren()
	-- no spawns in the folder: invalid argument #1 to 'random' (interval is empty)
	return spawns[math.random(#spawns)]
end

print(pickSpawn())

Check the length first and return nil when there is nothing to pick.

Fixed Script in ServerScriptService named Loot
local spawnFolder = workspace:WaitForChild("LootSpawns")

local function pickSpawn(): Instance?
	local spawns = spawnFolder:GetChildren()
	if #spawns == 0 then
		warn("[Loot] LootSpawns is empty")
		return nil
	end
	return spawns[math.random(#spawns)]
end

print(pickSpawn())

The smallest number is bigger than the largest

With two numbers, the first must not be bigger than the second. This happens when the numbers come from settings or attributes that someone set the wrong way round, or from math such as math.random(level, 10) once a player passes level 10.

Breaks Script in ServerScriptService named Rewards
local function rollReward(level)
	-- at level 12: math.random(12, 10), invalid argument #2 to 'random' (interval is empty)
	return math.random(level, 10)
end

print(rollReward(12))

Put the two numbers in order, or clamp the first one, so the range always holds at least one number.

Fixed Script in ServerScriptService named Rewards
local MAX_REWARD = 10

local function rollReward(level: number): number
	local low = math.min(level, MAX_REWARD)
	return math.random(low, MAX_REWARD)
end

print(rollReward(12))

A weighted pick with a total of zero

A weighted loot table rolls a number up to the sum of the weights. If every weight is 0, or the table is empty, the total is 0 and math.random(0) fails.

Breaks Script in ServerScriptService named Drops
local drops = {
	{ name = "Sword", weight = 0 },
	{ name = "Shield", weight = 0 },
}

local total = 0
for _, drop in drops do
	total += drop.weight
end

-- total is 0: invalid argument #1 to 'random' (interval is empty)
local roll = math.random(total)
print(roll)

Check the total before rolling and handle "nothing can drop" on its own.

Fixed Script in ServerScriptService named Drops
type Drop = { name: string, weight: number }

local drops: { Drop } = {
	{ name = "Sword", weight = 0 },
	{ name = "Shield", weight = 0 },
}

local function pickDrop(): string?
	local total = 0
	for _, drop in drops do
		total += drop.weight
	end
	if total <= 0 then
		return nil -- nothing can drop
	end
	local roll = math.random(total)
	for _, drop in drops do
		roll -= drop.weight
		if roll <= 0 then
			return drop.name
		end
	end
	return nil
end

print(pickDrop())

Fix it in your place

Paste the error, RoCode fixes it in your place

Copy the line from Output and paste it here. RoCode opens the script it names in the place you have open in Studio, works out which of the causes above it is, and edits the code there instead of handing you a snippet to paste.

For example: ServerScriptService.Loot:8: invalid argument #2 to 'random' (interval is empty)

RoCode is an AI agent for Roblox Studio: it reads the scripts in the place you have open, finds the line and edits it there, and every change is an undo point. It runs on Windows or macOS with a desktop browser. Free to start, with free credits every day.

How to find the line that causes it

  1. Open Output (Window menu, or the Script tab), clear it with Ctrl+K (⌘K on Mac), and press Play.
  2. Find the first red line. It starts with the script's place in the Explorer and the line number, such as ServerScriptService.Shop:12:.
  3. Click the red line. Studio opens that script with the cursor on the failing line.
  4. Read the lines between Stack Begin and Stack End under it: they list the calls that led there, newest first, so you can see which function called the broken one.
  5. Still not sure which value is wrong? Add print(typeof(x), x) just above the line, or set a breakpoint on it and read the values in the Watch window.

Questions

What does math.random(5) return?

A whole number from 1 to 5, both included. math.random(2, 5) returns 2 to 5, and math.random() with no numbers returns a decimal from 0 up to, but not including, 1.

How do I pick a random item from a table?

Check that it is not empty, then use list[math.random(#list)]. For a folder, use folder:GetChildren() to get the list first.

Why does it only fail sometimes?

Because the range is only empty at some moments: the list is empty after every item was collected, or the level only passes the maximum late in the game. Test with an empty folder and with the highest values on purpose to see it straight away.

Can RoCode fix it?

Yes. Paste the Output line into RoCode. It reads the script in your open place, finds the empty range and adds the check.

Sources and further reading

How the code was checked: every block marked Fixed passes Luau's strict type checker against Roblox's API definitions (luau-lsp, 14 November 2026), and every block marked Breaks compiles and fails only when it runs. The message wording was checked by running the broken code with the Luau command line tool and against the message strings in Roblox Studio itself (macOS build installed 12 November 2026) on 14 November 2026. A type check catches misspelt APIs and wrong types, not game logic, so playtest in Studio before you publish.

More Roblox errors

All Roblox Studio errors

Stop hunting errors line by line.

RoCode reads the scripts in your open place, fixes the error where it happens and tells you what it changed. Free every day, no card required.