【发布时间】:2019-10-14 16:10:53
【问题描述】:
我有两个 .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
预期结果是播放器屏幕上的“您已收到...”通知,但实际结果是错误:
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
由于我不知道的原因,经济.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)
【问题讨论】:
-
您的初始元表是否定义了
__index?喜欢meta.__index = meta
标签: lua garrys-mod