lua - LUA|MTASA 尝试索引字段“?” (零值)
问题描述
我的 MTA 服务器的 lua 脚本有问题。当我运行代码时,我收到以下错误:
attempt to index field '?' (a nil value)
这是代码:
addEvent("bank:transfer", true)
addEventHandler("bank:transfer", root, function(id, amount, to, reason)
if(transferBank(id, amount, to, reason)) then
triggerClientEvent(client, "bank:transferRecieve", client, true)
else
triggerClientEvent(client, "bank:transferRecieve", client, false)
end
end)
function transferBank(id, amount, to, reason)
if(id and amount and to and reason) then
if(BANK_ACCOUNTS[to]) then
if(BANK_ACCOUNTS[id].balance >= amount) then
dbExec(connection, "INSERT INTO bank_records (bank_id, record_type, record_from, reason, amount, date) VALUES(?, ?, ?, ?, ?, NOW())", to, 3, id, reason, amount)
dbExec(connection, "UPDATE bank_accounts SET balance = balance - ? WHERE id=?", amount, id)
dbExec(connection, "UPDATE bank_accounts SET balance = balance + ? WHERE id=?", amount, to)
BANK_ACCOUNTS[to].balance = BANK_ACCOUNTS[to].balance + amount
BANK_ACCOUNTS[id].balance = BANK_ACCOUNTS[id].balance - amount
return true
else
return false, "Le Compte Bancaire spécifié ne contient pas assez d'argent."
end
else
return false, "Le Compte Bancaire spécifié n'existe pas."
end
else
return false, "Argument Invalide."
end
end
我一直在寻找几个小时,但我找不到错误来自哪里。
解决方案
导致此错误消息的最小示例:
local BANK_ACCOUNTS = {}
BANK_ACCOUNTS[to].balance = 1
在这种情况下,BANK_ACCOUNTS[to] 为 nil,因此您无法对其进行索引。至于也是 nil Lua 没有字段名称/键放入错误消息中。
BANK_ACCOUNTS["test"].balance = 1
会给你以下错误信息:
attempt to index field 'test' (a nil value)
因此,在某些时候,to 或 id 都为零。
if BANK_ACCOUNTS[to] then
因为如果它必须是to
你就不会进入nil
id
推荐阅读
- oracle - 与最接近的匹配交叉应用
- c++ - 何时使用空白 cin.get()?
- c++ - 如何typedef模板函数指针?
- raspberry-pi - Raspberry Pi 脚本在启动后不启动
- java - 带有时尚 MNIST 数据集的 TensorFlow 模型
- laravel - 如何通过分页获取 Laravel Eloquent 中的特定列?
- tensorflow - tensorflow.python.framework.errors_impl.InvalidArgumentError:不兼容的形状:[1568] vs. [32,49]
- networking - 如何查找手机与连接的wifi路由器的距离?
- python - 如果由函数导入,则使用 `exec` 导入的模块不可用
- python - Pandas 数据类型转换 - 如何区分逗号和空格 -