• Give armor instead of set armor?
    27 replies, posted
I was wondering how to do [URL="https://facepunch.com/showthread.php?t=1406284&highlight=give+health"]this[/URL] with armor. This didn't work [CODE] local plyArmor = ply:Armor() ply:SetArmor(math.Clamp(plyArmor + 50, 0, ply:GetMaxArmor())) [/CODE] Thanks in advance. EDIT: I´m trying to add options to buy 50 health/armor to [URL="http://steamcommunity.com/sharedfiles/filedetails/?id=699996132"]this[/URL] 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:
[QUOTE=Sir TE5T;51243847][lua] local a = FindMetaTable( "Player" ) a:GetMaxArmor = function() return 200 end [/lua] [lua] ply:SetArmor( math.Clamp( ply:Armor() + 50, 0, 200 ) ) [/lua] :smile:[/QUOTE] When I use this code, the npc doesn't spawn anymore.
Nobody knows? It´s not so hard is it?
Is GetMaxArmor defined?
[QUOTE=Skere_;51244381]Is GetMaxArmor defined?[/QUOTE] 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
[CODE] 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 [/CODE] Then just use Player:GiveArmor( insertamount ) anywhere (be sure it's server side -_-)
[QUOTE=Tupac;51244593][CODE] 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:GetArmor() + 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 [/CODE] Then just use Player:GiveArmor( insertamount ) anywhere (be sure it's server side -_-)[/QUOTE] So this is the part of my code. [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[/CODE] What am I doing wrong?
[QUOTE=merkur0;51244642]So this is the part of my code. [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[/CODE] What am I doing wrong?[/QUOTE] 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.
[QUOTE=merkur0;51244642]So this is the part of my code. [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[/CODE] What am I doing wrong?[/QUOTE] 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.
[QUOTE=merkur0;51245167]So what should the correct code look like? Because changing the current code lead me to the npc not spawning again.[/QUOTE] 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.
[QUOTE=Tupac;51245219]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.[/QUOTE] 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 [B]does[/B] receive the net message because the NPC says "Here you go! Thanks for purchasing armor." when I click the "Buy 50 armor" button.
[QUOTE=Sir TE5T;51243847] local a = FindMetaTable( "Player" )a:GetMaxArmor = function() return 200 end ply:SetArmor( math.Clamp( ply:Armor() + 50, 0, 200 ) ) :smile:[/QUOTE] 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
OK, I don't know much about lua. Does it mean you are not gonna help me? I thought that's the purpose of this forum. I have no idea how to make it work, why can't you just give me the working code?
Because we were used to work for ourselves to get the code working without asking anyone else to give us code working That's not how do you learn, that's how do you get spoon feed
[QUOTE=gonzalolog;51249301]Because we were used to work for ourselves to get the code working without asking anyone else to give us code working That's not how do you learn, that's how do you get spoon feed[/QUOTE] But I did exactly what you guys said and it´s still not working.
this is the only thing weird i find function PLAYER:GiveArmor( 50 ) remove that 50, everything else just looks good, or you're running it in clientside, or it just won't work, we can't help you, because we can't control your computer or your brain
Well, it still won't give me the armor. [URL="https://www.mediafire.com/folder/980d326cbt3d9/addons_npc_healthandarmor"]This[/URL] is the addon, the code you gave me is inside init.lua, now you can see I'm not doing anything wrong. EDIT: I just noticed it gives me this error in the server console. [CODE] addons/addons_npc_healthandarmor/lua/entities/healthnpc/init.lua:132: attempt to call method 'GetArmor' (a nil value) [/CODE]
[QUOTE=merkur0;51249383]Well, it still won't give me the armor. [URL="https://www.mediafire.com/folder/980d326cbt3d9/addons_npc_healthandarmor"]This[/URL] is the addon, the code you gave me is inside init.lua, now you can see I'm not doing anything wrong. EDIT: I just noticed it gives me this error in the server console. [CODE] addons/addons_npc_healthandarmor/lua/entities/healthnpc/init.lua:132: attempt to call method 'GetArmor' (a nil value) [/CODE][/QUOTE] I don't see the GetArmor function included in any of your files... You can't call a function that doesn't exist.
[QUOTE=merkur0;51249383]Well, it still won't give me the armor. [URL="https://www.mediafire.com/folder/980d326cbt3d9/addons_npc_healthandarmor"]This[/URL] is the addon, the code you gave me is inside init.lua, now you can see I'm not doing anything wrong. EDIT: I just noticed it gives me this error in the server console. [CODE] addons/addons_npc_healthandarmor/lua/entities/healthnpc/init.lua:132: attempt to call method 'GetArmor' (a nil value) [/CODE][/QUOTE] Yea, that's an issue on my part. Change self:GetArmor() to self:Armor() on line 132.
[QUOTE=Tupac;51249753]Yea, that's an issue on my part. Change self:GetArmor() to self:Armor() on line 132.[/QUOTE] Thanks, it works now :)
Sorry, you need to Log In to post a reply to this thread.