Roblox Studio script analysis

Global 'x' is only used in the enclosing function; consider changing it to local

What Global 'x' is only used in the enclosing function; consider changing it to local 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 readScript AnalysisLuauWritten by

In short

Inside a function you wrote score = 10 without local, and nothing outside that function uses score. Without local, Luau makes it a global: one variable shared by every call of the function and the rest of the script. Script Analysis is suggesting the fix: put local in front of the first assignment. If the value really must be shared, declare it with local at the top of the script instead.

  • Add local where the variable is first set.
  • A global keeps its value between calls, so two players or two calls can overwrite each other's value.
  • The defined at line N form is the same warning for a function with no name, such as one passed to Connect.
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)
Global 'score' is only used in the enclosing function 'addPoints'; consider changing it to local

Other forms of the same message:

  • Global 'total' is only used in the enclosing function defined at line 4; consider changing it to local

In Luau, a name you assign without local is a global for that script. Globals are slower to read than locals, and they leak: the value stays after the function returns, and any other function can change it. Script Analysis noticed that this one is only used inside a single function, so it should be local to it.

It is a warning, not an error, and the script runs. It is worth fixing because it often hides real bugs, such as a Touched handler that mixes up two players because they share one variable.

Causes and fixes

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

local was left out inside a function

The most common case: a helper variable created inside a function without local.

Breaks ModuleScript in ServerScriptService named Points
local function addPoints(player, amount)
	-- Global 'score' is only used in the enclosing function 'addPoints'
	score = player:GetAttribute("Score") or 0
	player:SetAttribute("Score", score + amount)
end

return addPoints

Add local. Nothing else changes, and the warning goes away.

Fixed ModuleScript in ServerScriptService named Points
local function addPoints(player: Player, amount: number)
	local current = player:GetAttribute("Score")
	local score = if typeof(current) == "number" then current else 0
	player:SetAttribute("Score", score + amount)
end

return addPoints

An event handler shares one global between players

In a handler passed to Connect, the warning names the line the function starts on. Here every touch writes the same global, so two players touching at once can swap results.

Breaks Script in ServerScriptService named Pad
local Players = game:GetService("Players")

workspace.Pad.Touched:Connect(function(hit)
	-- Global 'toucher' is only used in the enclosing function defined at line 3
	toucher = Players:GetPlayerFromCharacter(hit.Parent)
	if toucher then
		toucher:SetAttribute("OnPad", true)
	end
end)

Make it local to the handler, so each touch gets its own variable.

Fixed Script in ServerScriptService named Pad
local Players = game:GetService("Players")

local pad = workspace:WaitForChild("Pad") :: BasePart

pad.Touched:Connect(function(hit: BasePart)
	local character = hit.Parent
	local toucher = if character and character:IsA("Model") then Players:GetPlayerFromCharacter(character) else nil
	if toucher then
		toucher:SetAttribute("OnPad", true)
	end
end)

The value was meant to last between calls

Sometimes the global is on purpose, such as a counter that should keep growing. It works, but a global is easy to overwrite by accident from anywhere in the script.

Breaks Script in ServerScriptService named Rounds
local function nextRoundNumber()
	roundNumber = (roundNumber or 0) + 1
	return roundNumber
end

print(nextRoundNumber(), nextRoundNumber())

Declare it once with local above the function. It still keeps its value between calls, and only code below it in this script can see it.

Fixed Script in ServerScriptService named Rounds
local roundNumber = 0

local function nextRoundNumber(): number
	roundNumber += 1
	return roundNumber
end

print(nextRoundNumber(), nextRoundNumber())

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: Global 'score' is only used in the enclosing function 'addPoints'; consider changing it to local

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

Does this warning break my script?

No. Script Analysis warnings do not stop anything. The script runs, but the global can cause bugs that are hard to find later.

Should I ever use globals in Roblox?

Rarely. Use local variables at the top of a script for values the whole script shares, and ModuleScripts for values several scripts share.

Where do I see Script Analysis?

In Studio, open it from the View tab. It lists warnings for every script without running the game, and the same problems are underlined in the Script Editor.

Can RoCode fix it?

Yes. Paste the warning into RoCode. It adds local in the script in your open place, or moves a shared value to the top of the script.

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.