This help-page, Help:Lua debugging, explains issues of writing Lua script and debugging the source code, to remove errors or improve performance. Because Lua is a "semi-compiled" interpreted language, it does not prescreen for all common syntax errors, nor detect misspelled variables, which are only found at runtime when seeing the "Script error" message. Also, unlike wp:templates which can be run interactively by edit-preview, the Lua script must be tested by show-preview (or run preview) of another page which uses a template which #invoke
s the Lua module being edited. In general, make one small change at a time, do a run show-preview, and save after a few good changes to have a version to restore, in case terrible errors occur after numerous later changes. Note: the saving of a working copy can be done to an offline text file, rather than saving each version into the module revisions.
Other pre-compiled, or semi-compiled, languages can pinpoint the line number, or code phrase, where a syntax error, or undefined variable name, has been detected; however, that processing can require extra time to perform. Instead, it might be possible to use a lexical analysis tool, specifically for Lua script, to better detect and pinpoint misspelled variables or logic errors in Lua source code.