Roblox 2009 Lua:获取加载字符串';s错误

Roblox 2009 Lua:获取加载字符串';s错误,lua,roblox,Lua,Roblox,我已经做了几个小时的2009年脚本生成器,但我不知道如何使它打印错误。如果我打印(loadstring(“a”),它将打印到roblox输出nil[string“s”]:1:'='预期在'附近,它==nil。我想要得到的是它在结尾报告的错误:“=”应该在“附近,type type是nil,所以我不知道如何得到它。如果有人能帮忙,我将不胜感激 请参阅Lua 5.1手册,该手册将为您提供以下文档: 如果有错误。。。返回nil加上错误消息 Lua通常会将错误消息作为第二个返回值返回: local f,

我已经做了几个小时的2009年脚本生成器,但我不知道如何使它打印错误。如果我打印(loadstring(“a”),它将打印到roblox输出
nil[string“s”]:1:'='预期在'
附近,它==nil。我想要得到的是它在结尾报告的错误:“=”应该在“附近,type type是nil,所以我不知道如何得到它。如果有人能帮忙,我将不胜感激

请参阅Lua 5.1手册,该手册将为您提供以下文档:

如果有错误。。。返回nil加上错误消息

Lua通常会将错误消息作为第二个返回值返回:

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

err
从错误发生的位置开始,它毫无帮助地引用了
loadstring
的输入

例如,对于输入代码
“hello there”
,错误为

[string "hello there"]:1: '=' expected near 'there'
[string "hello..."]:2: '=' expected near 'there'
[string "helloooooooooooooooooooooooooooooooooooooooooooooooooooooo ther..."]:1: '=' expected near 'there'
Lua似乎在第一个换行符或63个字符处截断引号,以较小者为准:

对于
“hello\there”
错误是

[string "hello there"]:1: '=' expected near 'there'
[string "hello..."]:2: '=' expected near 'there'
[string "helloooooooooooooooooooooooooooooooooooooooooooooooooooooo ther..."]:1: '=' expected near 'there'
对于
“helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo

[string "hello there"]:1: '=' expected near 'there'
[string "hello..."]:2: '=' expected near 'there'
[string "helloooooooooooooooooooooooooooooooooooooooooooooooooooooo ther..."]:1: '=' expected near 'there'
如果您确定脚本的前63个字符/第一行中没有
“]:
,则只需搜索该序列即可找到其停止位置:

local location, message = err:match('^(%[string ".*"%]:%d+:%s+)(.*)$')
如果您的代码是,例如,
“hello\”]:1:there“
,您可能需要对其进行寻址,则这将是不正确的

解决这一问题的最简单方法是将用户控制权从引用的第一行移开:在代码前面加上您自己的第一行,这很好(如果您向用户显示错误,请确保调整错误的行号:)

现在,错误消息应该始终开始

[string "--code..."]:

OP可能想要
err:match(“:(*)”)
。他们说“type是nil”,所以看起来他们不知道第二个返回值。但是,是的,我会稍微介绍一下如何处理错误