Jump to content

Healthbar over player's vehicle


NadhmiNK

Recommended Posts

Posted

Hi

So, what i'm trying to accomplish is pretty simple but i can't seem to do it :cry:. I want to show a healthbar over other players' vehicles.

Anywat, I've made this simple script:

addEventHandler( "onClientRender", root, 
   function( thePlayer ) 
          local vehicle = getPedOccupiedVehicle(thePlayer) 
          local x, y, z = getElementPosition( vehicle ) 
          local sX, sY = getScreenFromWorldPosition( x, y, z ) 
          local health = math.floor( getElementHealth( vehicle )/10) 
          local Rhealth = 50 
          local lineLength = 56 * ( health / 100 ) 
            if health < 25 then 
                health = 0 
            else 
                math.floor( getElementHealth( vehicle )/10) 
            end 
          if isElementOnScreen( vehicle ) then 
           if sX then 
              dxDrawText( health, sX, sY ) 
              dxDrawRectangle( sX-32, sY-82, 60, 14, tocolor( 0, 100, 0, 120 ) ) 
              dxDrawRectangle( sX-30, sY-80, 56, 10, tocolor( 0, 220, 0, 120 ) ) 
              dxDrawRectangle( sX-30, sY-80, lineLength, 10, tocolor( 0, 220, 0 ) ) 
           end 
          end 
   end 
) 

But it doesn't act as any other floating health bar (that one witch is on top of every player's car). I can see my own car's bar but i can't see other's ones (same thing with other players)

So, can anyone can help me out fixing this script, or give me another fully working one :fadein:

Thnx;

Posted

Try this:

  
addEventHandler( "onClientRender", root, 
   function() 
          for i, vehicle in ipairs(getElementsByType("vehicle")) do 
              local x, y, z = getElementPosition( vehicle ) 
              local sX, sY = getScreenFromWorldPosition( x, y, z ) 
              local health = math.floor( getElementHealth( vehicle )/10) 
              local Rhealth = 50 
              local lineLength = 56 * ( health / 100 ) 
                if health < 25 then 
                    health = 0 
                else 
                    math.floor( getElementHealth( vehicle )/10) 
                end 
              if isElementOnScreen( vehicle ) then 
               if sX then 
                  dxDrawText( health, sX, sY ) 
                  dxDrawRectangle( sX-32, sY-82, 60, 14, tocolor( 0, 100, 0, 120 ) ) 
                  dxDrawRectangle( sX-30, sY-80, 56, 10, tocolor( 0, 220, 0, 120 ) ) 
                  dxDrawRectangle( sX-30, sY-80, lineLength, 10, tocolor( 0, 220, 0 ) ) 
               end 
            end 
        end 
   end 
) 
  

Those changes should render the HP bar for all the vehicles.

Posted

Works, thanks. BUT, it shows the health bar for every vehicle in the map whereas i only want it for the players' vehicles (ped's occupied vehicle).

Can you help me with that?

Posted

This should work.

  
addEventHandler( "onClientRender", root, 
   function() 
          for i, vehicle in ipairs(getElementsByType("vehicle")) do 
              local x, y, z = getElementPosition( vehicle ) 
              local sX, sY = getScreenFromWorldPosition( x, y, z ) 
              local health = math.floor( getElementHealth( vehicle )/10) 
              local Rhealth = 50 
              local lineLength = 56 * ( health / 100 ) 
                if health < 25 then 
                    health = 0 
                else 
                    math.floor( getElementHealth( vehicle )/10) 
                end 
              local vehicleDriver = getVehicleOccupant ( vehicle ) -- get driver of car 
              if isElement(vehicleDriver) then -- is there driver in car?, if yes draw the healthbar 
              if isElementOnScreen( vehicle ) then 
               if sX then 
                  dxDrawText( health, sX, sY ) 
                  dxDrawRectangle( sX-32, sY-82, 60, 14, tocolor( 0, 100, 0, 120 ) ) 
                  dxDrawRectangle( sX-30, sY-80, 56, 10, tocolor( 0, 220, 0, 120 ) ) 
                  dxDrawRectangle( sX-30, sY-80, lineLength, 10, tocolor( 0, 220, 0 ) ) 
               end 
            end 
            end 
        end 
   end 
) 
  

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