1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Sign up for news, events and more!

    You're currently visiting the official DarkRP Forums as a guest. Sign up now to participate in our community and we'll let you know when we have news.

Modify health when picking up a weapon

Discussion in 'Modder General Chat' started by Goran, May 24, 2020.

  1. Goran

    Goran New Member

    Hi again.

    Im new at lua and programming, and i need a code that lows player hp when picking up certain weapon.

    Any help? Thank you.
     
  2. Zaktak

    Zaktak Member

    Although this will essentially work, you'd wanna add some more checks. Ex: If ( !IsValid(ply) ) then return end etc..
    Code (Text):
    hook.Add( "WeaponEquip", "GenericNameHook", function(wep, ply)
        if ( wep:GetPrintName() == "specialWeapon" ) then
            local var = 50 -- How much to lower
            ply:SetHealth( ply:Health() - var )
        end
    end)
     
    Goran likes this.
  3. Goran

    Goran New Member

    Thank you. If I want to set it to happen only if hp is equal or bigger than 50?


    I tried to do it by myself, but got lua error, if you want to, can you tell me what is wrong with this code?

    hook.Add("WeaponEquip", "akhplow", function(player , weapon)
    if (player:Health >= 51) then
    var = 50
    if (weapon:printName()== "ISIS AK-47") then
    player:SetHealth(player:Health - var)

    end
    end
    end)

    Also, when you pick up the weapon two times, hp gets to 0 but don't die.
    Again, thank you, sorry for not posting everything at once.
     
    Last edited: May 26, 2020
  4. Andorek

    Andorek New Member

    Wherever there is player:Health, you need to change it to player:Health()
     
    Goran likes this.
  5. Goran

    Goran New Member

    Got it, thank you.
     
  6. Zaktak

    Zaktak Member

    You want to check if player:Health() - var is smaller or equal to zero. If it is, you want to kill the player ( player:Kill() ).
     
    Goran likes this.

Share This Page