如何解决“尝试调用方法'addMoney'(nil值)'错误?

问题描述 投票:0回答:1

我有两个.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

在经济方面。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 

出于我的一个未知原因,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)
lua garrys-mod
1个回答
0
投票

问题是,economy.lua是在客户端执行的,而我的代码是在服务器端的。

© www.soinside.com 2019 - 2024. All rights reserved.