PcoWSkbVqDnWTu_dm2ix

Script Analysis Tool

Script Analysis Tool

10 min

Roblox Studio contains a static script analyzer that validates code, flags all issues that will completely prevent the script from running, and warns you about issues that are likely to cause a bug. To display the script analysis panel, toggle Script Analysis from the View tab:

Warnings

In addition to script-halting errors, the analyzer may report the following warnings:

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. Normally you would discover this error by running the code, but the script analyzer highlights it as a likely problem:

StarterGui.LocalScript
W001: (3,7) Unknown global 'displaName'

W002: Global 'old' is deprecated, use 'new' instead

Roblox exposes several built-in global variables to scripts. Some of them are deprecated, so you should use the non-deprecated alternative when prompted. For example, API naming conventions in Roblox declare game as the correct reference, while the capitalized Game is deprecated:

StarterGui.LocalScript
W002: (1,14) Global 'Game' is deprecated, use 'game' instead

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

As mentioned above, Lua identifiers are treated as global by default. Whenever possible, you should declare a variable/function with local, but it’s easy to forget this practice and make everything global. This can lead to further issues when scripting, for instance a global variable declaration overwriting an identically-named function.

To assist you with local variable declarations, the script analyzer issues a warning if a global variable is only used in one function:

StarterGui.LocalScript
W003: (4,3) Global 'b' is only used in the enclosing function; consider changing it to local

To resolve this, simply forward-declare the variable b above the conditional statement where it’s used:

W006: Statement spans multiple lines; use indentation to silence

Lua does not require semicolons to end lines/statements, nor does it prevent you from breaking a statement into multiple lines. This can lead to confusing code, for instance:

StarterGui.LocalScript
W006: (5,1) Statement spans multiple lines; use indentation to silence

Whenever you have a legitimate use for breaking statements into multiple lines, you should indent your code to make the intent clear:

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. Overwriting them is possible due to Lua’s dynamic nature, but it can lead to critical bugs like this:

StarterGui.LocalScript
W010: (3,2) Built-in global 'wait' is overwritten here; consider using a local or changing the name

To fix this, either put the wait variable into local scope or change its name:

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, such as when a function returns multiple results but you only need one of them (line 7). However, attempting to output the variable (line 9) will result in a warning, so choose a more descriptive name than _ if you need to use the variable in any manner.

StarterGui.LocalScript
W011: (9,7) Placeholder value '_' is read here; consider using a named variable

W012: Unreachable code

If any statement in your code is “unreachable,” the analyzer will warn you as follows:

StarterGui.LocalScript
W012: (7,2) Unreachable code (previous statement always returns)

W013: Unknown type 'name'

Various API calls rely on passing a class name as a string. To help prevent bugs, the analyzer verifies that the given type exists. If you see a warning like this, fix the type.

StarterGui.LocalScript
W013: (1,27) Unknown type 'Oart'
W013: (3,15) Unknown type 'Vector3' (expected primitive type)

W014: For loop should iterate backwards; did you forget to specify -1 as step?

If you want to iterate backwards in a for loop, such as count down from 5 to 1, it’s easy to forget the -1 step parameter. The analyzer will flag these occurrences as follows:

StarterGui.LocalScript
W014: (1,9) For loop should iterate backwards; did you forget to specify -1 as step?

W015: Assigning [num] values to [num] variables...

If you declare multiple variables in one statement, but do not assign the same number of values, the analyzer will either prompt you to assign nil to extra variables or remind you that some values are unused:

StarterGui.LocalScript
W015: (1,1) Assigning 2 values to 3 variables initializes extra variables with nil; add 'nil' to value list to silence
W015: (2,1) Assigning 3 values to 2 variables leaves some values unused
Tags:
  • coding
  • lua
  • error
  • warning