如何修复“尝试调用方法 'addMoney'(零值)”错误?
How to fix 'attempt to call method 'addMoney' (a nil value)' error?
我有两个 .lua 文件,一个名为 sv_money.lua,另一个名为 economy.lua
sv_money.lua中的代码是:
local meta = FindMetaTable("Player")
local PlayersMoney = {}
function meta:addMoney(amount)
if not amount then return false end
PlayersMoney[self:SteamID64()] = PlayersMoney[self:SteamID64()] + amount
end
并且在 economy.lua 中:
for k, v in pairs(player.GetAll()) do
v:addMoney(60)
Notify(v, "You've received 60€.", 5, "Generic" )
end
预期结果是播放器屏幕上的通知 "You've received..." 但实际结果是错误:
Lua Error: [ERROR] addons/economy/lua/autorun/economy.lua:63: attempt to call method 'addMoney' (a nil value)
1. unknown - addons/economy/lua/autorun/economy.lua:63
出于未知原因,economy.lua 中的这段代码有效:
hook.Add("PlayerSpawnProp", "Cost", function(ply, class)
if not ply:canAfford(10) then
Notify(ply, "You don't have enough money !", 5, "Error" )
return false
else
Notify(ply, "You've spent 10€ to spawn this prop.", 5, "Generic" )
ply:addMoney(-10)
return true
end
end)
问题是 economy.lua 是在客户端执行的,而我的代码是在服务器端执行的。
我有两个 .lua 文件,一个名为 sv_money.lua,另一个名为 economy.lua
sv_money.lua中的代码是:
local meta = FindMetaTable("Player")
local PlayersMoney = {}
function meta:addMoney(amount)
if not amount then return false end
PlayersMoney[self:SteamID64()] = PlayersMoney[self:SteamID64()] + amount
end
并且在 economy.lua 中:
for k, v in pairs(player.GetAll()) do
v:addMoney(60)
Notify(v, "You've received 60€.", 5, "Generic" )
end
预期结果是播放器屏幕上的通知 "You've received..." 但实际结果是错误:
Lua Error: [ERROR] addons/economy/lua/autorun/economy.lua:63: attempt to call method 'addMoney' (a nil value)
1. unknown - addons/economy/lua/autorun/economy.lua:63
出于未知原因,economy.lua 中的这段代码有效:
hook.Add("PlayerSpawnProp", "Cost", function(ply, class)
if not ply:canAfford(10) then
Notify(ply, "You don't have enough money !", 5, "Error" )
return false
else
Notify(ply, "You've spent 10€ to spawn this prop.", 5, "Generic" )
ply:addMoney(-10)
return true
end
end)
问题是 economy.lua 是在客户端执行的,而我的代码是在服务器端执行的。