Lua尝试对全局“金额”(零值)执行算术运算

时间:2018-11-14 16:39:16

标签: lua

当我尝试使用脚本功能时,出现此错误:

attempt to perform arithmetic on global 'amount' (a nil value)

这是函数:

<>
function openAdvertisements( player, command )
    local advertisements = { } --These will hold our advertisements to send to the client and populate our advertisement tables.

    if not player then player = source end

    --Fetch all of the advertisements from the database --mysql:query("SELECT * FROM 'advertisements' WHERE 1")
    for _, ad in ipairs( call(getResourceFromName("mysql"), "query_free", "UPDATE `accounts` SET `dm`=`dm`-".. amount*2000 .." WHERE `id`=".. tostring(gameAccountID)  .."")  ) do
        if tonumber( ad.expiry ) >= tonumber( getRealTime().timestamp ) then --Check if the advertisement has expired, delete it if so.
            ad.author = exports.mysql:select_one( "characters", { id = ad.created_by } ).charactername
            table.insert( advertisements, ad )
        else
            deleteAdvertisement( ad.id )
        end
    end

    triggerClientEvent( player, resourceName .. ":display_all", root, advertisements, exports.integration:isPlayerAdmin( player ) ) --Send the advertisements to the client to create the GUI.
end

错误与该行有关:

for _, ad in ipairs( call(getResourceFromName("mysql"), "query_free", "UPDATE `accounts` SET `dm`=`dm`-".. amount*2000 .." WHERE `id`=".. tostring(gameAccountID)  .."")  ) do

1 个答案:

答案 0 :(得分:0)

您尚未定义局部amount变量,因此lua在全局表(_ENV_G中,取决于lua版本)中查找它,该变量也不存在(nil用于不存在的密钥)。

Lua试图执行nil*2000,导致该错误。