简体   繁体   中英

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)

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

Also, I'd recommend using ipairs here instead of pairs since the table indices of player.GetAll() are numbers and not strings.

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.

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.

Consider running this script entirely serverside, or alternatively, add if(SERVER)then checks on parts of code that is only supposed to run serverside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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