0

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
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
  • Define what "works perfectly fine" means, i assume you expect it to equip some armor? are you certain it is doing what you expect? that error would mean `v` does not have a function call `SetArmor` – Nifim Mar 09 '21 at 22:55
  • are you sure the error occurs in the posted code? or maybe somewhere else? – Piglet Mar 10 '21 at 10:18
  • It does what it must do, it regens armor. But if ```v``` doesn't have this function why it works then? Error occurs in game console – normaizbieny Mar 10 '21 at 14:56
  • well it doesnt work anymore – normaizbieny Mar 10 '21 at 16:04

2 Answers2

0

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.

Toast
  • 13
  • 3
0

I assume you are running the code both serverside and clientside. The error you receive is probably clientside (i.e. 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.

olawrdhalpme
  • 180
  • 8