【问题标题】:LUA|MTA attempt to index global "zoneName" (a nil value)LUA|MTA 尝试索引全局“zoneName”(零值)
【发布时间】:2020-03-24 20:08:28
【问题描述】:

我在学习的 Lua 脚本中遇到了一个问题(我是 Lua 新手),这个错误让我非常困惑,当我运行代码时,它给了我以下错误:

attempt to index global "zoneName" (a nil value)

这是我的代码:

    local zoneName = zoneName:gsub ( "'", "" )  
    if dbExec ( handler, "INSERT INTO `safeZones` (`rowID`, `zoneName`, `zoneX`, `zoneY`, `zoneWidth`, `zoneHeight`) VALUES (NULL, '".. tostring ( zoneName ) .."', '".. tostring ( zoneX ) .."', '".. tostring ( zoneY ) .."', '".. zoneWidth .."', '".. zoneHeight .."');" ) then 
    createSafeZone (            {               [ "zoneName" ] = zoneName,              [ "zoneX" ] = zoneX,                [ "zoneY" ] = zoneY,                [ "zoneWidth" ] = zoneWidth,                [ "zoneHeight" ] = zoneHeight           }       )      
    outputDebugString ( "Safe Zones: Safe zone created name: ".. tostring ( zoneName ) ) 
    return true 
       else        
           return false, "Unable to create the safe zone" 
           end

【问题讨论】:

  • 你正在尝试 zoneName:gsubzoneName==nil
  • 嗯,我猜你是对的,但我仍然没有弄清楚问题所在,我应该发布整个代码吗?
  • 我解决了,@EgorSkriptunoff 感谢您的帮助

标签: lua lua-table


【解决方案1】:

你已经在它的定义中引用了 zoneName,你的代码等于

本地 zoneName = nil:gsub("'", "")

因此出现错误(当 Lua 尝试执行 zoneName:gsub() 时 zoneName 尚未定义)。

在 gsub() 调用之前定义 zoneName 或使用 string.gsub()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 2014-01-26
    • 1970-01-01
    • 2017-12-20
    • 2019-03-01
    • 1970-01-01
    • 2013-06-07
    相关资源
    最近更新 更多