Roblox 2009 Lua: Get Loadstrings error

Roblox 2009 Lua: Get Loadstrings error

Refer to the Lua 5.1 manual, which will point you to the documentation for load:

If there [are] errors, … returns nil plus the error message.

Its typical for Lua to return error messages as a second return value:

local f, err = loadstring(mycode)
if not f then
    print(There was an error: ` .. err .. `)

This err begins with where the error happened, which unhelpfully quotes the input to loadstring.

For example, for the input code hello there, the error is

[string hello there]:1: = expected near there

Lua appears to cut the quote off at the first newline or 63 character, whichever is less:

For hellothere the error is

[string hello...]:2: = expected near there

For helloooooooooooooooooooooooooooooooooooooooooooooooooooooo there the error is

[string helloooooooooooooooooooooooooooooooooooooooooooooooooooooo ther...]:1: = expected near there

If youre sure theres no ]: in the first 63 characters/first line of your script, you can just search for that sequence to find where it stops:

local location, message = err:match(^(%[string .*%]:%d+:%s+)(.*)$)

This wont be right if your code is, for example, hello]:1: there, which you may want to address.

The simplest way to address it would be to take user control away from the first line that is quoted: prepend the code with your own first line that is nice (and make sure to adjust the line number of the error if you display it to the user:)

local f, err = loadstring(--coden .. mycode)

Now the error message should always begin

[string --code...]:

Roblox 2009 Lua: Get Loadstrings error

Leave a Reply

Your email address will not be published. Required fields are marked *