Script analysis

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[edit]

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:

ScriptAnalysisToolbar.png

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

Warnings[edit]

This section describes every warning that the analyzer can report.

W001: Unknown global 'name'[edit]

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:

print(orkspace.Part.CFrame)

Normally you would discover this error by running the code; the 'orkspace' 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[edit]

ROBLOX exposes several built-in global variables to scripts.

Some of them are deprecated - just don't use them. Some of them have multiple names, e.g. "Game" and "game". API naming conventions in ROBLOX declare "game" as correct spelling - if you use an alternative spelling, you'll get a warning:

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

You're supposed to fix the warning by changing 'Game' to 'game'.

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

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
else
    b = 2
end
print (b)

to this:

local b
if a < 5 then
    b = 1
else
    b = 2
end
print (b)

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[edit]

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'[edit]

Various calls in the ROBLOX API rely on you passing a type 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[edit]

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()
...
   spawn = 1
   print(spawn)
...
end
 
...
 
spawn(function() print('Hello') end)

The spawn() call at the end will only work if DoSomething() function is not called - calling DoSomething() overrides the spawn global to refer to a number. You should change 'spawn' in DoSomething to be a local variable and consider renaming it to make the code less ambiguous.

W011: Placeholder value '_' is read here; consider using a named variable[edit]

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 '_'.