Roblox Studio script analysis

Variable 'x' shadows previous declaration at line N

What Variable 'x' shadows previous declaration at line N means in Roblox Studio, the two things that cause it with the code that triggers each, and the fix for each one.

Updated 14 November 20263 min readScript AnalysisLuauWritten by

In short

You wrote local part = ... while another local part from the line in the message is still in scope. The new one hides (shadows) the old one until its block ends, so code inside the block can no longer reach the first value. Often that is fine, but it is a common source of bugs: you meant to change the old variable and made a new one instead. Rename the new variable, or drop the second local if you meant to reuse the first.

  • The line number in the message is where the first variable was declared.
  • Drop local to change the existing variable; rename it to keep both.
  • It is a warning: the script runs either way.
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

Script Analysis (example)
Variable 'part' shadows previous declaration at line 1

A local lives until the end of the block it was declared in (a function, a loop, an if). Declaring another local with the same name inside that block creates a second, separate variable. Luau's linter calls this shadowing and reports it, because the two are easy to mix up.

Luau tools report it as LocalShadow. You may see it in Studio's Script Analysis or in an editor that runs luau-lsp, such as Visual Studio Code.

Causes and fixes

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

You meant to change the old variable

The classic bug: an extra local inside an if or a loop creates a new variable, and the outer one never changes.

Breaks ModuleScript in ServerScriptService named Winner
local function findWinner(players)
	local best = nil
	local bestScore = -1
	for _, p in players do
		local score = p:GetAttribute("Score") or 0
		if score > bestScore then
			-- shadows: the outer best and bestScore never change
			local best = p
			local bestScore = score
			print(best, bestScore)
		end
	end
	return best
end

return findWinner

Remove the inner local so the assignment updates the variables declared above.

Fixed ModuleScript in ServerScriptService named Winner
local function findWinner(players: { Player }): Player?
	local best: Player? = nil
	local bestScore = -1
	for _, p in players do
		local value = p:GetAttribute("Score")
		local score = if typeof(value) == "number" then value else 0
		if score > bestScore then
			best = p
			bestScore = score
		end
	end
	return best
end

return findWinner

A loop variable reuses a name from outside

Loops are where shadowing bites most. A loop variable with the same name as a local above it hides that local for the whole loop, so code inside the loop that meant the outer object gets each item instead.

Breaks Script in ServerScriptService named Keys
local part = workspace:WaitForChild("Door")

for _, part in workspace:GetChildren() do
	-- shadows line 1: inside the loop, part is each child, not the Door
	if part.Name == "Key" then
		print("a key is in the workspace, the door is", part.Name)
	end
end

Give each one a name that says what it is. The loop reads clearly and both values stay reachable.

Fixed Script in ServerScriptService named Keys
local door = workspace:WaitForChild("Door")

for _, child in workspace:GetChildren() do
	if child.Name == "Key" then
		print("a key is in the workspace, the door is", door.Name)
	end
end

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: Variable 'part' shadows previous declaration at line 1

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 Script Analysis from the View tab. It lists warnings for every script in the place without running anything.
  2. Each row names the script and the line. Double-click it to open the script there.
  3. The same problem is underlined in the Script Editor; hover over the underline to read it.

Questions

Is shadowing always a bug?

No. A short-lived variable with the same name in a small block can be fine. The warning is there because it is a bug often enough to check each one.

How do I turn the warning off?

Rename the variable, which is usually clearer. To silence it for a whole script, put --!nolint LocalShadow on the first line.

Why does the line number point somewhere else?

It is the line where the first, hidden variable was declared. The warning itself is underlined on the new declaration.

Can RoCode fix it?

Yes. Paste the warning into RoCode. It reads the script in your open place, works out whether you meant to reuse or rename the variable, and fixes it.

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 against the message strings in Roblox Studio itself (macOS build installed 12 November 2026) and reproduced with luau-analyze and luau-lsp 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.