Jump to content

health and Armor In DX


#RooTs

Recommended Posts

what's wrong with this my function, the same happened with Armor ( it was for working with perfection )

if getElementHealth() ~= 0 then 
  
    if getElementHealth() == 5 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/05.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 10 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/10.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 15 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/15.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 20 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/20.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 25 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/25.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 30 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/30.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 35 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/35.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 40 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/40.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 45 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/45.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 50 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/50.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 55 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/55.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 60 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/60.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 65 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/65.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 70 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/70.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 75 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/75.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 80 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/80.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 85 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/85.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 90 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/90.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 95 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/95.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    if getElementHealth() == 100 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/100.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
        end 
end 

Edited by Guest
Link to comment
  • Moderators
local health = math.ceil(getElementHealth(localPlayer)) 
if health > 0 then 
  
    if health <= 5 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/05.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    elseif health <= 10 then 
  
-- etc. 

Link to comment

By looking at the logic you are drawing images when HP is equal to a number, which is hard to achieve in normal server conditions without using runcode or other health manipulation. So if your health is let's say 50 HP it will draw the image, but if it is 51 it will draw nothing.

To solve it just do more checking:

  
if getElementHealth (localPlayer) > 0 and getElementHealth (localPlayer) <= 5 then 
    --draw function 
elseif getElementHealth (localPlayer) > 5 and getElementHealth (localPlayer) <= 10 then 
  -- etc etc 
  

Link to comment
local health = math.ceil(getElementHealth(localPlayer)) 
if health > 0 then 
  
    if health <= 5 then 
        dxDrawImage(x*0.785, y*0.200, 30, 35, "health/05.png", 0, 0, 0, tocolor(0, 0, 0, 255), true) 
  
    elseif health <= 10 then 
  
-- etc. 

@IIYAMA

I will try your example, :wink:

Link to comment

@IIYAMA Not Work :(

local largura = 220 
local altura = 250 
-------- 
local Tlargura = 0.785 
local Taltura = 0.200 
  
         
  
local health = math.ceil(getElementHealth(localPlayer)) 
if health > 0 then 
  
    if health <= 5 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/05.png" ) 
  
  
    if health <= 10 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/10.png" ) 
  
  
    if health <= 15 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/15.png" ) 
  
  
    if health <= 20 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/20.png" ) 
  
  
    if health <= 25 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/25.png" ) 
  
  
    if health <= 30 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/30.png" ) 
  
  
    if health <= 35 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/35.png" ) 
  
  
    if health <= 40 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/40.png" ) 
  
  
    if health <= 45 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/45.png" ) 
  
  
    if health <= 50 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/50.png" ) 
  
  
    if health <= 55 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/55.png" ) 
  
  
    if health <= 60 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/60.png" ) 
  
  
    if health <= 65 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/65.png" ) 
  
  
    if health <= 70 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/70.png" ) 
  
  
    if health <= 75 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/75.png" ) 
  
  
    if health <= 80 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/80.png" ) 
  
  
    if health <= 85 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/85.png" ) 
  
  
    if health <= 90 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/90.png" ) 
  
  
    if health <= 95 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/95.png" ) 
  
  
    if health <= 100 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/100.png" ) 
    end 
end 

Link to comment
  
local largura = 220 
local altura = 250 
-------- 
local Tlargura = 0.785 
local Taltura = 0.200 
  
         
  
local health = math.ceil(getElementHealth(localPlayer)) 
    if health > 0 then return 
  
  
    elseif health <= 5 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/05.png" ) 
  
  
    elseif health <= 10 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/10.png" ) 
  
  
    elseif health <= 15 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/15.png" ) 
  
  
    elseif health <= 20 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/20.png" ) 
  
  
    elseif health <= 25 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/25.png" ) 
  
  
    elseif health <= 30 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/30.png" ) 
  
  
    elseif health <= 35 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/35.png" ) 
  
  
    elseif health <= 40 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/40.png" ) 
  
  
    elseif health <= 45 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/45.png" ) 
  
  
    elseif health <= 50 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/50.png" ) 
  
  
    elseif health <= 55 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/55.png" ) 
  
  
    elseif health <= 60 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/60.png" ) 
  
  
    elseif health <= 65 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/65.png" ) 
  
  
    elseif health <= 70 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/70.png" ) 
  
  
    elseif health <= 75 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/75.png" ) 
  
  
    elseif health <= 80 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/80.png" ) 
  
  
    elseif health <= 85 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/85.png" ) 
  
  
    elseif health <= 90 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/90.png" ) 
  
  
    elseif health <= 95 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/95.png" ) 
  
  
    elseif health <= 100 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/100.png" ) 
end 
  

not sure

Link to comment
  
local largura = 220 
local altura = 250 
-------- 
local Tlargura = 0.785 
local Taltura = 0.200 
  
         
  
local health = math.ceil(getElementHealth(localPlayer)) 
    if health > 0 then return 
  
  
    elseif health <= 5 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/05.png" ) 
  
  
    elseif health <= 10 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/10.png" ) 
  
  
    elseif health <= 15 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/15.png" ) 
  
  
    elseif health <= 20 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/20.png" ) 
  
  
    elseif health <= 25 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/25.png" ) 
  
  
    elseif health <= 30 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/30.png" ) 
  
  
    elseif health <= 35 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/35.png" ) 
  
  
    elseif health <= 40 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/40.png" ) 
  
  
    elseif health <= 45 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/45.png" ) 
  
  
    elseif health <= 50 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/50.png" ) 
  
  
    elseif health <= 55 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/55.png" ) 
  
  
    elseif health <= 60 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/60.png" ) 
  
  
    elseif health <= 65 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/65.png" ) 
  
  
    elseif health <= 70 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/70.png" ) 
  
  
    elseif health <= 75 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/75.png" ) 
  
  
    elseif health <= 80 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/80.png" ) 
  
  
    elseif health <= 85 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/85.png" ) 
  
  
    elseif health <= 90 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/90.png" ) 
  
  
    elseif health <= 95 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/95.png" ) 
  
  
    elseif health <= 100 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/100.png" ) 
end 
  

not sure

not work :(:(

Link to comment

Or instead of doing all of that 30-60 times per second, you could do this:

1. Use the Client-Side event onClientPlayerDamage. This event triggers when a player is damaged.

2. Isolate it to the local player, thus it only runs the code on your client if YOU were damaged.

function exampleCode_Client(theAttacker, theWeapon, theBodyPart) 
    if(source == localPlayer) then 
        outputChatBox("You were damaged!", 187, 0, 0, true); 
    end 
end 
addEventHandler("onClientPlayerDamage", root, exampleCode_Client); 

3. Fetch the player's new health after being damaged, store it in a local variable.

4. Update the rendered image depending on current health. Use <= (Less than or equal to) and >= (Greater than or equal to).

local dxHealthImage = "health/100.png"; 
  
function exampleCode_Client(theAttacker, theWeapon, theBodyPart) 
    if(source == localPlayer) then 
        outputChatBox("You were damaged!", 187, 0, 0, true); 
        local localPlayerHealth = getElementHealth(localPlayer); 
        if(localPlayerHealth >= 96) then                                -- Is the health between 96-100+? Draw 100.png 
            dxHealthImage = "health/100.png"; 
        elseif(localPlayerHealth <= 95 and localPlayerHealth > 91) then -- Is the health between 95 and 91? Draw 95.png 
            dxHealthImage = "health/95.png"; 
        elseif(localPlayerHealth <= 90 and localPlayerHealth > 86) then -- Is the health between 90 and 86? Draw 90.png 
            dxHealthImage = "health/90.png"; 
        end 
    end 
end 
addEventHandler("onClientPlayerDamage", root, exampleCode_Client); 

Link to comment

that's what I did, and still does not work :( can make things easier for me?

local x,y = guiGetScreenSize() 
  
function hudLekRoots ( ) 
  
local largura = 220 
local altura = 200 
-------- 
local Tlargura = 0.830 
local Taltura = 0.001 
  
local dxHealthImage = "health/100.png"; 
  
function exampleCode_Client(theAttacker, theWeapon, theBodyPart) 
    if(source == localPlayer) then 
        outputChatBox("You were damaged!", 187, 0, 0, true); 
        local localPlayerHealth = getElementHealth(localPlayer); 
  
  
        if(localPlayerHealth >= 96) then                                -- Is the health between 96-100+? Draw 100.png 
dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/100.png" ) 
            dxHealthImage = "health/100.png"; 
  
  
        elseif(localPlayerHealth <= 95 and localPlayerHealth > 91) then -- Is the health between 95 and 91? Draw 95.png 
dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/95.png" ) 
            dxHealthImage = "health/95.png"; 
  
        elseif(localPlayerHealth <= 90 and localPlayerHealth > 86) then -- Is the health between 90 and 86? Draw 90.png 
dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/90.png" ) 
            dxHealthImage = "health/90.png"; 
  
  
        end 
    end 
end 
addEventHandler("onClientPlayerDamage", root, exampleCode_Client); 
  
  
  
end 
  
  
local hudTable = { 
  
"ammo", 
"armour", 
"clock", 
"health", 
"money", 
"weapon", 
"wanted", 
  
"area_name", 
"vehicle_name", 
"breath", 
"clock" 
} 
  
addEventHandler("onClientRender", root, hudLekRoots) 
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
    for id, hudComponents in ipairs(hudTable) do 
        showPlayerHudComponent(hudComponents, false) 
    end 
    end 
) 
  
addEventHandler("onClientResourceStop", resourceRoot, 
    function() 
    for id, hudComponents in ipairs(hudTable) do 
        showPlayerHudComponent(hudComponents, true) 
    end 
    end 
) 
  
  

Link to comment
local health = math.ceil(getElementHealth(localPlayer)) 
    if health > 0 then return 

not sure

if health > 0 then return, really? Return if they're alive? That doesn't seem very productive.

local health = math.ceil(getElementHealth(localPlayer)) 
    if health > 0 then  
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/"..(math.ceil(getElementHealth(localPlayer)/5)*5)..".png" ) 
end 

This code will do what the previous 20 iterations of elseif's was meant to do, and the only compromise is changing the health/05.png file to health/5.png.

Link to comment

@Hex547

Working, Thanks my friend, it help me a lot. function simple and easy.

have to make things easier for newbies like me :lol::lol::lol:

local health = math.ceil(getElementHealth(localPlayer)) 
    if health > 0 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/"..(math.ceil(getElementHealth(localPlayer)/5)*5)..".png" ) 
end 
  
  
local armor = math.ceil(getPedArmor(localPlayer)) 
    if health > 0 then 
        dxDrawImage(x*Alargura, y*Aaltura, largura, altura, "armor/"..(math.ceil(getPedArmor(localPlayer)/5)*5)..".png" ) 
end 
  

Link to comment
  
local health = math.ceil(getElementHealth(localPlayer)) 
    if health > 0 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/"..(math.ceil(getElementHealth(localPlayer)/5)*5)..".png" ) 
end 
  
  
local armor = math.ceil(getPedArmor(localPlayer)) 
    if health > 0 and armor > 0 then 
        dxDrawImage(x*Alargura, y*Aaltura, largura, altura, "armor/"..(math.ceil(getPedArmor(localPlayer)/5)*5)..".png" ) 
end 
  
  

To check if there is armor!

Link to comment
  
local health = math.ceil(getElementHealth(localPlayer)) 
    if health > 0 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/"..(math.ceil(getElementHealth(localPlayer)/5)*5)..".png" ) 
end 
  
  
local armor = math.ceil(getPedArmor(localPlayer)) 
    if health > 0 and armor > 0 then 
        dxDrawImage(x*Alargura, y*Aaltura, largura, altura, "armor/"..(math.ceil(getPedArmor(localPlayer)/5)*5)..".png" ) 
end 
  
  

To check if there is armor!

Why would you check if the player's health is over 0 in the armor's function? :|

Link to comment
  
local health = math.ceil(getElementHealth(localPlayer)) 
    if health > 0 then 
        dxDrawImage(x*Tlargura, y*Taltura, largura, altura, "health/"..(math.ceil(getElementHealth(localPlayer)/5)*5)..".png" ) 
end 
  
  
local armor = math.ceil(getPedArmor(localPlayer)) 
    if health > 0 and armor > 0 then 
        dxDrawImage(x*Alargura, y*Aaltura, largura, altura, "armor/"..(math.ceil(getPedArmor(localPlayer)/5)*5)..".png" ) 
end 
  
  

To check if there is armor!

Why would you check if the player's health is over 0 in the armor's function? :|

Common sense, When I died in a server, Health was 0 and armor was thier and when I spawned armor was 0.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...