1. 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.

SetHealth/MaxHealth Error

Discussion in 'DarkRP Modding Questions & Help' started by TheTrustySword, Jul 25, 2017.

  1. TheTrustySword

    TheTrustySword New Member

    Hey, I've encountered an error while attempting to set the health of certain jobs. I have tried setting the health of one job by using this code:

    PlayerLoadout = function(ply)
    ply:SetHealth(750)
    ply:SetMaxHealth(750)
    end,

    My Problem is that it does not change the max health or current health of the job I have added it onto. It, instead changes the health of ALL the jobs under this in the code. So I now have many jobs with 750 health but the job I intended to have at 750 health is at the defualt 100 health.

    If any one knows what I can do please set me some help :)
     
  2. Sir Klutch

    Sir Klutch Active Member

    SetMaxHealth() is server-sided. It cannot run clientsided like it is trying to do thus throwing the error that it is nil. SetHealth(), on the other hand, should work perfectly fine. Just go ahead and remove the PlayerSpawn function from the jobs as I will be setting the players health for that specific job below.

    You can try something like this;
    Code (Lua):

    local Teams_MaxHealth = {
        [TEAM_NAME] = {500, 250}, -- First value (500) is the max health. Second value (250) is the current health.
        [TEAM_NAME2] = {750, 375}, -- First value (750) is the max health. Second value (375) is the current health.
    }

    local function SetTeams_MaxHealth(ply, _, _)
        local MaxHealth_Teams = Teams_MaxHealth[ply:Team()]

        if MaxHealth_Teams then
            ply:SetMaxHealth(Teams_MaxHealth[1])
            ply:SetHealth(Teams_MaxHealth[2]) -- Might as well set their health here rather than on the job...
        end
    end

    hook.Add("OnPlayerChangedTeam", "set max health for team", SetTeams_MaxHealth)
     
    This should set their max health and health when they switch to that specific team.

    Configure the teams and max health/current health values and place in lua/autorun/server
     
    Last edited: Jul 25, 2017
  3. TheTrustySword

    TheTrustySword New Member

    The Code doesn't appear to work. I have put it in lua/autorun/server but the healths for everything are just 100 now.
     
  4. Sir Klutch

    Sir Klutch Active Member

    Did you configure the teams?
     
  5. TheTrustySword

    TheTrustySword New Member

    As in the [TEAM_NAME] = {500, 250}, -- First value (500) is the max health. Second value (250) is the current health.
    [TEAM_NAME2] = {750, 375}, -- First value (750) is the max health. Second value (375) is the current health.
    bit. I have done that unless there is something else to do.
     
  6. Sir Klutch

    Sir Klutch Active Member

    Try this;

    Code (Lua):

    local Teams_MaxHealth = {
        [TEAM_NAME] = {500, 250}, -- First value (500) is the max health. Second value (250) is the current health.
        [TEAM_NAME2] = {750, 375}, -- First value (750) is the max health. Second value (375) is the current health.
    }

    local function SetTeams_MaxHealth(ply, _, _)
        local MaxHealth_Teams = Teams_MaxHealth[ply:Team()]

        if MaxHealth_Teams then
             timer.Simple(0.1, function()
                   ply:SetMaxHealth(Teams_MaxHealth[1])
                   ply:SetHealth(Teams_MaxHealth[2]) -- Might as well set their health here rather than on the job...

              end)
        end
    end

    hook.Add("OnPlayerChangedTeam", "set max health for team", SetTeams_MaxHealth)
     
     
  7. TheTrustySword

    TheTrustySword New Member

    Still does the same thing. Our Code is as follows:

    Code (Text):
    local Teams_MaxHealth = {
        [SUPREME_GENERAL] = {800, 800},
        [BATTALION_GENERAL] = {750, 750},
    }

    local function SetTeams_MaxHealth(ply, _, _)
        local MaxHealth_Teams = Teams_MaxHealth[ply:Team()]

        if MaxHealth_Teams then
             timer.Simple(0.1, function()
                   ply:SetMaxHealth(Teams_MaxHealth[1])
                   ply:SetHealth(Teams_MaxHealth[2])

              end)
        end
    end

    hook.Add("OnPlayerChangedTeam", "set max health for team", SetTeams_MaxHealth), "set max health for team", SetTeams_MaxHealth)
     
  8. Sir Klutch

    Sir Klutch Active Member

    Whoops... Try that.
    Code (Lua):

    local Teams_MaxHealth = {
        [TEAM_HOBO] = {500, 250}, -- First value (500) is the max health. Second value (250) is the current health.
    }

    local function SetTeams_MaxHealth(ply, _, _)
        local MaxHealth_Teams = Teams_MaxHealth[ply:Team()]

        if MaxHealth_Teams then
            timer.Simple(0.1, function()
                ply:SetMaxHealth(MaxHealth_Teams[1])
                ply:SetHealth(MaxHealth_Teams[2]) -- Might as well set their health here rather than on the job...
            end)
        end
    end

    hook.Add("OnPlayerChangedTeam", "set max health for team", SetTeams_MaxHealth)
     
     
  9. TheTrustySword

    TheTrustySword New Member

    Still not working. I have no clue what it could be...
     
  10. Sir Klutch

    Sir Klutch Active Member

    Odd... Works perfectly fine on my end.
     
  11. TheTrustySword

    TheTrustySword New Member

    And just to clarify: I copy your code exactly and change the job names and the numbers and it should work?
     
  12. Sir Klutch

    Sir Klutch Active Member

    Post your version, please.
     
  13. TheTrustySword

    TheTrustySword New Member

    Apologies, I am on holiday and have little time with internet access. My code is as follows.
    Code (Text):
    local Teams_MaxHealth = {
        [SUPREME_GENERAL] = {800, 800},
        [TEAM_GENERAL] = {750, 750}
    }

    local function SetTeams_MaxHealth(ply, _, _)
        local MaxHealth_Teams = Teams_MaxHealth[ply:Team()]

        if MaxHealth_Teams then
            timer.Simple(0.1, function()
                ply:SetMaxHealth(MaxHealth_Teams[1])
                ply:SetHealth(MaxHealth_Teams[2])
            end)
        end
    end

    hook.Add("OnPlayerChangedTeam", "set max health for team", SetTeams_MaxHealth)
     
  14. Sir Klutch

    Sir Klutch Active Member

    Try this...

    Code (Text):
    local Teams_MaxHealth = {
        [SUPREME_GENERAL] = {800, 800},
        [TEAM_GENERAL] = {750, 750}
    }

    local function SetTeams_MaxHealth(ply, _, _)
        local MaxHealth_Teams = Teams_MaxHealth[ply:Team()]

        if MaxHealth_Teams then
            timer.Simple(0.1, function()
                ply:SetMaxHealth(Teams_MaxHealth[1])
                ply:SetHealth(Teams_MaxHealth[2])
            end)
        end
    end

    hook.Add("OnPlayerChangedTeam", "set max health for team", SetTeams_MaxHealth)
     
  15. # VALERY

    # VALERY New Member

    This is jobs.lua from my darkrp server
    Code (Text):

    TEAM_ALPHA = DarkRP.createJob("Alpha", {
        color = Color(30, 30, 60, 255),
        model = {
        "models/mw2/skin_03/MW2_Soldier_01.mdl",
        "models/mw2/skin_03/MW2_Soldier_02.mdl",
        "models/mw2/skin_03/MW2_Soldier_03.mdl",
        "models/mw2/skin_03/MW2_Soldier_04.mdl",
        "models/mw2/skin_03/MW2_Soldier_05.mdl",
        "models/mw2/skin_03/MW2_Soldier_06.mdl"
        },
        description = [[]],
        weapons = {"wep_jack_job_drpradio", "rfplay_door_ram", "rfplay_weaponchecker", "tfa_m92beretta", "tfa_mp5"},
        command = "alpha",
        max = 4,
        salary = 950,
        admin = 0,
        vote = false,
        hasLicense = true,
        PlayerLoadout = function(ply) ply:SetHealth(125) ply:SetArmor(100) end, -- !
        PlayerSpawn = function(ply) ply:SetMaxHealth("125") end -- !
    })
     
     

Share This Page