Give armor instead of set armor?

I was wondering how to do this with armor.

This didn’t work



local plyArmor = ply:Armor()
ply:SetArmor(math.Clamp(plyArmor + 50, 0, ply:GetMaxArmor()))


Thanks in advance.

EDIT: I´m trying to add options to buy 50 health/armor to this addon

[lua]
local a = FindMetaTable( “Player” )
a:GetMaxArmor = function() return 200 end
[/lua]
[lua]
ply:SetArmor( math.Clamp( ply:Armor() + 50, 0, 200 ) )
[/lua]
:smile:

When I use this code, the npc doesn’t spawn anymore.

Nobody knows? It´s not so hard is it?

Is GetMaxArmor defined?

I dont think so.

Define it yourself in that case. Either use a number, or a function like SIR TE5T made if you plan on reusing it

Basically choose a maximum

Well, the Sir´s code didn’t work, what code do I use then?

Replace the last argument of math.Clamp with a number that represents the maximum armor



local PLAYER            = FindMetaTable( 'Player' ) -- Finds the players meta table
local max               = 200 -- Set this to the max armor you want the player to be able to have

function PLAYER:GiveArmor( amt ) -- This code can only be ran on players, as it is in the "Player" meta table
      self:SetArmor( math.Clamp( self:Armor() + amt, 0, max ) ) -- Sets the armor to the players current armor + the amount you input, but doesn't allow it to go past the "max" variable you set in the beginning
end



Then just use Player:GiveArmor( insertamount ) anywhere (be sure it’s server side -_-)

So this is the part of my code.


net.Receive( "GiveArmorNPC50", function( len, ply )
local PLAYER            = FindMetaTable( 'Player' ) -- Finds the players meta table
local max               = 100 -- Set this to the max armor you want the player to be able to have

function PLAYER:GiveArmor( 50 ) -- This code can only be ran on players, as it is in the "Player" meta table
      self:SetArmor( math.Clamp( self:GetArmor() + 50, 0, max ) ) -- Sets the armor to the players current armor + the amount you input, but doesn't allow it to go past the "max" variable you set in the beginning
end
	if ply:Armor() < 100 then
		if ply.DarkRPVars.money > 500 then
			ply:PrintMessage(HUD_PRINTTALK, "Here you go! Thanks for purchasing armor.")
			ply:addMoney(-500)
			Player:GiveArmor(50)
		else
			ply:PrintMessage(HUD_PRINTTALK, "Sorry! You don't have enough money to buy armor.")
		end
	else
		ply:PrintMessage(HUD_PRINTTALK, "You have full armor! I refuse to give you more.")
	end

What am I doing wrong?

Although this wont change much, but keep the code i gave you on it’s own (outside the net.receive function)

when are you sending the net message? and to be sure that it’s being received, add print( ‘received net message’ ) to the beginning of the function to make sure it’s being received by the server and check the server console.

Your problem is you do Player:GiveArmor, while the it should be ply:GiveArmor.

And please, dont create the metamethod inside a net message, please

So what should the correct code look like? Because changing the current code lead me to the npc not spawning again.

try piecing the posts above together instead of asking to be spoonfed please. I already gave you code and explained what to do, and so did a couple of other people.

I changed Player:GiveArmor to ply:GiveArmor and I moved the metamethod outside of the net message but it still didn’t work. And the server does receive the net message because the NPC says “Here you go! Thanks for purchasing armor.” when I click the “Buy 50 armor” button.

I love how literally almost every thread I go in to, I see Sir TEST rating people dumb, rather than posting an alternative, but when he posts garbage, everybody else rates him. :v:

This is facepunch, and there is nobody who knows such a simple thing?

Code seems to be working on anyone eyes, but you’re the dumb one here that can’t make it work