Tabletop Simulator

Tabletop Simulator

Not enough ratings
[Scripting] Understanding Lua Errors
By Bone White
Explains common scripting errors in TTS with examples
   
Award
Favorite
Favorited
Unfavorite
Error messages explained
Scripiting errors in TTS will look something like the below. They are printed red in Game chat, and also in red in the console. The look like this:

Error in Script (abc123): chunk_4:(2,1-3): attempt to call a nil value

Error in Script (id)
The value between the brackets here is the GUID of the Object that owns the script with the error. If the script that has the error is Global, then it will show Global instead of an Object GUID.

chunk_n:(a,b-c)
* n is not useful to us
* a is the line which was running when the error was caused
* b-c are the start and end characters on that line which were being run when the error was caused

: everything after the second colon
This section tells us what the error is (if known). See below for more information on specific errors.
IMPORTANT - Error line accuracy
The error line is not always accurate, if you cannot immediately see what the issue is, I recommend adding a log line before and after the reported line to double-check it is accurate.

If I get an error on the following line:
x() -- attempt to call a nil value

I will often first run something like this to confirm that the error is on that line:

log("before x()") x() -- attempt to call a nil value log("after x()")

If I see the first log line but not the second, I have proved the error is caused between the two.
Error: attempt to [x] a [y] value
This error occurs when you are trying to use a variable in a way that is invalid for that variable type.

Explanation

[x]
x tells you how you are trying to use the variable. Examples of x are:
* call
* index
* perform arithmetic on

[y]
y tells you what type the variable is:
* nil
* boolean
* number
* string
* table
* function
* thread
* userdata

Examples

attempt to call a nil value

You are trying to call () a value as if it was a function, but the value is nil.

example code:
local x = nil x() -- attempt to call a nil value

attempt to index a string value

You are trying to index [] or . a value as if it was a table, but the value is a string.

example code:
local s = "Hello World!" s.something -- attempt to index a string value

local s = "Hello World!" s["something"] -- attempt to index a string value

attempt to perform arithmetic on a userdata value

You are trying to perform arithmetic + - * / % ^ (and more) on a value as if it was a number (or a string that can be treated as a number), but the value is userdata.

example code:
local obj = getObjectFromGUID("abc123") local x = obj + 5 -- attempt to perform arithmetic on a userdata value

Note
userdata and tables with metamethods may be used in ways that are unusual, and may not result in typical errors. For example you can perform some arithmetic on a Player Color[api.tabletopsimulator.com] table. This depends entirely on how they were designed.
Error: Pattern too complex
This error occurs when you are using a Lua string function that uses patterns (string.match, string.find, string.gmatch, string.gsub).

Explanation

This is an error caused by the Moonsharp implementation of Lua that Tabletop Simulator uses. There is no definitive answer as to why this happens. You can run a simple pattern on a large string and get the error, but run the same pattern on a smaller string and not get the error.

Workarounds

  • Use patterns that will result in shorter matches
  • Split your string up into smaller strings and then use your string function on each of those individually; combine your results afterwards if required
  • Use a non-pattern method of parsing your string