简体   繁体   English

尝试调用方法“SetArmor”(零值)

[英]Attempt to call method 'SetArmor' (a nil value)

So script works perfectly fine, but every time it does it's part there appears an error in console which is says this:所以脚本工作得非常好,但每次它都是它的一部分,控制台中会出现一个错误,它是这样说的:

attempt to call method 'SetArmor' (a nil value)尝试调用方法“SetArmor”(零值)

Here's the code这是代码

local function ArmorRegeneration ()
  for k,v in pairs( player.GetAll() ) do
    if (v:IsValid()) then
      if v:Alive() and v:Armor() < 150 and ( !v.lastregen or v.lastregen < CurTime() - 1 ) then
        v.lastregen = CurTime()
        v:SetArmor( v:Armor() + 1 )
      end
    end
  end
end

Is this script supposed to be server side instead of client side?这个脚本应该是服务器端而不是客户端吗? I've never done Garry's Mod development, but I figure this might be a similar issue: https://stackoverflow.com/a/58381218/3150484我从来没有做过 Garry 的 Mod 开发,但我认为这可能是一个类似的问题: https://stackoverflow.com/a/58381218/3150484

Also, I'd recommend using ipairs here instead of pairs since the table indices of player.GetAll() are numbers and not strings.另外,我建议在这里使用ipairs而不是pairs ,因为player.GetAll()的表索引是数字而不是字符串。

I assume you are running the code both serverside and clientside.我假设您正在运行服务器端和客户端的代码。 The error you receive is probably clientside (ie SetArmor is not a clientside function. You can't set a player's armor clientside. So, it returns nil), but what is working is the serverside part of the code, which is why the code still appears fine in-game.您收到的错误可能是客户端错误(即 SetArmor 不是客户端 function。您无法设置玩家的装甲客户端。因此,它返回 nil),但有效的是代码的服务器端部分,这就是代码的原因在游戏中看起来仍然很好。

If I remember correctly, clientside errors are displayed with yellow text in Garry's Mod, so that might be something to look out for to confirm this.如果我没记错的话,客户端错误会在 Garry's Mod 中以黄色文本显示,因此可能需要注意确认这一点。

Consider running this script entirely serverside, or alternatively, add if(SERVER)then checks on parts of code that is only supposed to run serverside.考虑完全在服务器端运行此脚本,或者添加if(SERVER)then检查只应该在服务器端运行的部分代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM