【问题标题】:Lua attempt to perform arithmetic on global 'amount' (a nil value)Lua 尝试对全局“数量”(零值)执行算术运算
【发布时间】:2018-11-14 16:39:15
【问题描述】:

当我尝试使用脚本函数时出现此错误:

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

ERROR 与这一行有关:

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

【问题讨论】:

  • 变量amount没有值,所以是nil,尝试计算nil*2000会报错,因为nil不是数字也不是可转换的字符串到一个数字。

标签: lua


【解决方案1】:

你还没有定义本地的amount变量,所以lua在全局表中寻找它(_ENV_G,取决于lua版本),它也不存在(nil非- 现有密钥)。

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

【讨论】:

  • 我发现了一个类似的话题,他们说我必须改变这个调用(getResourceFromName("mysql"), "query_free", "UPDATE accounts SET dm=dm- ".. amount*2000 .." WHERE id=".. tostring(gameAccountID) .."") 到这个 mysql:query("SELECT * FROM 'advertisements' WHERE 1") 但现在我有另一个错误尝试索引全局“mysql”(一个零值)
猜你喜欢
  • 2015-11-07
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 2014-02-17
  • 2020-08-22
  • 2020-04-17
  • 1970-01-01
  • 2017-04-06
相关资源
最近更新 更多