LUA|MTA 试图索引全局变量"zoneName" (一个空值)

我在学习 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, "无法创建安全区"
           end
点赞
用户2420301
用户2420301

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

local zoneName = nil:gsub("'", "")

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

要么在gsub()调用之前定义zoneName,要么使用string.gsub()

2020-03-26 11:07:37