Storing health values (SWEP Help)

Hi, I’m editing the default DarkRP med_kit SWEP. I am also using the Vrondakis leveling system. What i’m trying to do is addXP based on the amount of health that is added. I’m not sure if I have to network this? or have the health added to the player stored in a table?

I already have it set up to add XP when the players health equals their max health, In this case, found is the player getting healed


        if found then
        local ply = self:GetOwner()
        found:SetHealth(found:Health() + 1)
        if found:Health() == found:GetMaxHealth() then
        ply:addXP(75)
        self:EmitSound("hl1/fvox/boop.wav", 150, found:Health() / found:GetMaxHealth() * 100, 1, CHAN_AUTO)
    end
        
    
    end
end

Here is the full code for the Medkit, All shared.


if SERVER then
    AddCSLuaFile("shared.lua")
end

SWEP.PrintName = "Medic Kit"
SWEP.Author = "DarkRP Developers"
SWEP.Slot = 4
SWEP.SlotPos = 0
SWEP.Description = "Heals the wounded."
SWEP.Contact = ""
SWEP.Purpose = ""
SWEP.Instructions = "Left click to heal someone
Right click to heal yourself"
SWEP.IsDarkRPMedKit = true

SWEP.Spawnable = true
SWEP.AdminOnly = true
SWEP.Category = "DarkRP (Utility)"

SWEP.ViewModel = "models/weapons/c_medkit.mdl"
SWEP.WorldModel = "models/weapons/w_medkit.mdl"
SWEP.UseHands = true

SWEP.Primary.Recoil = 0
SWEP.Primary.ClipSize  = -1
SWEP.Primary.DefaultClip = 1
SWEP.Primary.Automatic  = true
SWEP.Primary.Delay = 0.1
SWEP.Primary.Ammo = "none"

SWEP.Secondary.Recoil = 0
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 1
SWEP.Secondary.Automatic = true
SWEP.Secondary.Delay = 0.1
SWEP.Secondary.Ammo = "none"

function SWEP:PrimaryAttack()
    self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)

    local found
    local lastDot = -1 -- the opposite of what you're looking at
    self:GetOwner():LagCompensation(true)
    local aimVec = self:GetOwner():GetAimVector()

    for k,v in pairs(player.GetAll()) do
        local maxhealth = v:GetMaxHealth() or 100
        if v == self:GetOwner() or v:GetShootPos():DistToSqr(self:GetOwner():GetShootPos()) > 7225 or v:Health() >= maxhealth or not v:Alive() then continue end

        local direction = v:GetShootPos() - self:GetOwner():GetShootPos()
        direction:Normalize()
        local dot = direction:Dot(aimVec)

        -- Looking more in the direction of this player
        if dot > lastDot then
            lastDot = dot
            found = v
        end
    end
    self:GetOwner():LagCompensation(false)

    if found then
        local ply = self:GetOwner()
        found:SetHealth(found:Health() + 1)
        if found:Health() == found:GetMaxHealth() then
        ply:addXP(75)
        self:EmitSound("hl1/fvox/boop.wav", 150, found:Health() / found:GetMaxHealth() * 100, 1, CHAN_AUTO)
    end
        
    
    end
end

function SWEP:SecondaryAttack()
    self:SetNextSecondaryFire(CurTime() + self.Secondary.Delay)
    local ply = self:GetOwner()
    local maxhealth = self:GetOwner():GetMaxHealth() or 100
    if ply:Health() < maxhealth then
        ply:SetHealth(ply:Health() + 1)
        self:EmitSound("hl1/fvox/boop.wav", 150, ply:Health() / ply:GetMaxHealth() * 100, 1, CHAN_AUTO)
    end
end


Any help would be appreciated! Thanks!

Have you tried it?

[editline]10th December 2016[/editline]

Oh and I didn’t initially read you’r question, let me check.

[editline]10th December 2016[/editline]


    if found then
        local ply = self:GetOwner()
        found:SetHealth(found:Health() + 1)
		
		if not ply._givexp then
			ply._givexp = ( ply._givexp + 1 )
		end
		
        if found:Health() == found:GetMaxHealth() then
			ply:addXP( ply._givexp )
                        ply._givexp = 0
			self:EmitSound("hl1/fvox/boop.wav", 150, found:Health() / found:GetMaxHealth() * 100, 1, CHAN_AUTO)
		end
    end

I think that should work.

I’ve tried something similar to this, the issue is that it will spam the player with “You got 1xp!” Thats why I was trying to get some info on how to store the variable, and when the player’s health reaches 100 it would give an amount of xp based on health given. An example, if a player’s health is at 20 and they get healed to 100, it gives 80xp, (HealthGiven-HealthBeforeHeal = AddXPAmount) w/o spamming the player. The issue lies in finding out how much health the player had and storing it. I haven’t tested your code but i’m almost certain it works, just with undesired results :(. Anyways, thanks for reply, I appreciate it! :v:

Alright, if it still don’t work. TrySetNWInt and GetNWInt.

Then you can network all the values, same as above but a little different here:


    if found then
        local ply = self:GetOwner()
        found:SetHealth(found:Health() + 1)
		
		if ply._givexp then
			ply:SetNWint( "getxp", ply:GetNWInt( "getxp"  ) + 1 ) 
                else
                      ply:SetNWint( "getxp", 1 ) 
		end
		
        if found:Health() == found:GetMaxHealth() then
			ply:addXP( ply:GetNWInt( "givexp"  ) )
                        ply:SetNWInt( "givexp", 0 ) 
			self:EmitSound("hl1/fvox/boop.wav", 150, found:Health() / found:GetMaxHealth() * 100, 1, CHAN_AUTO)
		end
    end

oh and I made an error in the p


    if found then
        local ply = self:GetOwner()
        found:SetHealth(found:Health() + 1)
		
		if ply._givexp then -- Shouldn't be not!
			ply._givexp = ( ply._givexp + 1 )
                else
                        ply.givexp = 1 -- this is added :D 
		end
		
        if found:Health() == found:GetMaxHealth() then
			ply:addXP( ply._givexp )
                        ply._givexp = 0
			self:EmitSound("hl1/fvox/boop.wav", 150, found:Health() / found:GetMaxHealth() * 100, 1, CHAN_AUTO)
		end
    end