We use cookies on this site to enhance your user experience

The Script Analysis Tool

The Script Analysis Tool

Sep 25 2019, 2:00 PM PST 5 min

Lua compiler does basic verification of your program before executing it, but it is very easy to accidentally make a mistake that can only be caught when running the script. While this is hard to avoid in general, other languages (i.e. C#) try harder to make sure the program does what you expect it to. Discovering errors at compile time helps since you don’t have to test all code paths to find trivial issues.

Roblox Studio implements a static analyzer that validates script code, flags all issues that would completely prevent the script from running as errors, and flags all issues that are likely to cause a bug when running the script as warnings.

Note that the analyzer is not perfect; there are two kinds of incorrect reports, false negatives and false positives.

False negative means that there is a bug in your code that the analyzer can not find.
False positive means that the analyzer highlights a possible issue in your code, but the code always works.

We’re trying to balance the analyzer so that it finds the maximum amount of issues in scripts (minimizing false negatives) and does it without introducing false positives, but it’s impossible to make it perfect - so sometimes you will encounter false positives. Usually it’s worth it to fix them as well as real issues to keep your code clean from warnings.

Displaying the analysis results

By default the panel that shows you errors and warnings in scripts is hidden. It is recommended that you keep this panel visible when working with scripts so that you can quickly identify and fix the issues as you work with the script code.

To show the panel, go to the View tab and toggle the Script Analysis button:


In addition to showing warnings in the panel, the script editor displays them using blue squiggly lines under the problematic code fragments.


This section describes every warning that the analyzer can report.

W001: Unknown global ‘name’

Any identifier in Lua is assumed to refer to a global variable. Unfortunately, this means that it’s easy to mistype a variable name and introduce a bug, for example:


Normally you would discover this error by running the code; the workspace identifier is now highlighted by script analyzer as a likely source of a problem.

To fix this error, you should know what your code is supposed to be doing:

  • Sometimes this is just a typo and you should correct the identifier name
  • Sometimes you meant to use a field in a table but forgot to specify the table name, i.e.
local obj = {}
obj.Name = "Object"
print(Name) -- should be obj.Name

W002: Global ‘name’ is deprecated

Roblox exposes several built-in global variables to scripts. Some of them are deprecated; use a non-deprecated alternative. Some global variables have multiple names, for example Game and game. API naming conventions in Roblox declare game as correct spelling, and the capitalized version is deprecated.

W002: Global 'Game' is deprecated, use 'game' instead

You can fix the warning by using the recommended variable here (change Game to game)

W003: Global name is only used in the enclosing function; consider changing it to local

As mentioned above, identifiers are treated as global variables by default in Lua. To make a local variable, you should declare it as such:

local var

It is easy to forget to do so for variables that could be local. This can lead to further issues when modifying the code - i.e. when you add more functions to your script that happen to use the same variable name unintentionally, forgetting to make them local can result in several functions interfering with each others state.

To avoid that, analyzer issues a warning if you have a global variable that’s only used in one function.

You should consider either changing it to a local variable, e.g. by changing code like this:

if a < 5 then
    b = 1
    b = 2

to this:

local b
if a < 5 then
    b = 1
    b = 2

Or, if the variable is expected to be global, you can assign it to nil or to any other value in the global scope (outside of the code in functions).

W006: Statement spans multiple lines; use indentation to silence

Lua does not require to end statements with a semicolon, but it does not have any rules that prevent you from breaking a statement into multiple lines. This can lead to confusing bugs. Consider this code:

local text = "Name: " .. -- player.Name
-- now show this text in a UI object
UpdateLabel(label, text)

Despite the fact that UpdateLabel is separated from the local variable assignment by a comment and a new line, this code is actually equivalent to this:

local text = "Name: " .. UpdateLabel(label, text)

This code calls UpdateLabel with nil instead of the text value.

The UpdateLabel call is thus flagged with the warning. Whenever you have a legitimate use for breaking statements into multiple lines, you have to indent your code to make the intent clear:

local text = "Name: " ..
    UpdateLabel(label, text) -- now the indentation structure clearly highlights the problem

W007: Unknown type name ‘name’

Various calls in the Roblox API rely on passing a class name as a string. To prevent typos, the analyzer verifies that the given type exists. If you have a warning like this, your code is most likely wrong and you should fix the type, i.e.

local part = Instance.new("Partt", workspace) -- should be "Part"

W010: Built-in global name is overwritten here; consider using a local or changing the name

Both Lua and Roblox define a certain set of global variables and functions. Overriding them is possible due to Lua’s dynamic nature but can lead to subtle bugs, i.e.:

function DoSomething()
   -- Here, we override the built-in function spawn. The analyzer will about this:
   spawn = 1
   -- Solution: rename "spawn" here to something different or declare it as a local variable

-- Somewhere else in your code, you might use the built-in function normally, but
-- get a run-time error because it was overridden to a number, not the original function.

Above, the call to spawn at the end only works if DoSomething function has never been called - calling DoSomething overrides the spawn global variable to a number. You should use a different name (here, rename spawn in DoSomething) to be a local variable and/or consider renaming it to make the code less ambiguous.

W011: Placeholder value _ is read here; consider using a named variable

An established convention in many languages, including Lua, is to use _ as a placeholder for a variable name - e.g. when you read several results from a function and only need some of them, you can write

local _, foo = bar()

Since the convention uses this name for variables that do not matter, reading this value will result in the warning above. If you need to use the variable, choose a descriptive name instead of _.

  • coding
  • lua
  • errors