Jump to content

Updating GUI onClientPlayerTarget event


Recommended Posts

Hello, I want to display the targets health on a text label inside of a window when I target an object or player, however it does not update when the health has changed. I am not sure which function / event I need to call to continue updating the gui window.

Here is my script function:

targetWindow = guiCreateWindow(0.25, 0.40, .2, .2, "", true) 
targetHealth = guiCreateLabel(0.15, 0.37, 1, 1, "", true, targetWindow) 
guiSetVisible(targetWindow, false) 
  
  
function guiTargetInformation(target) 
    if (target) then 
        guiSetText(targetHealth, "Health:"..getElementHealth(target)) 
        guiSetVisible(targetWindow, true) 
    else 
        guiSetVisible(targetWindow, false) 
    end 
end 
  
addEventHandler("onClientPlayerTarget", getRootElement(), guiTargetInformation) 

Link to comment

Add timer like

updateTimer = setTimer(updateMyTarget,500,0) -- into onClientPlayerTarget 

function updateMyTarget() 
      local mytarget = getPedTarget(getLocalPlayer()) 
      if myTarget then 
             --getElementHealth and other stuff to update 
      else 
             killTimer(updateTimer) 
             updateTimer = nil 
      end 
end 

Even another more efficient method is to add 'onClientPlayerDamage' event, if you need to update only health:

addEventHandler('onClientPlayerDamage',target,updateMyTarget) 

and of course remove it when target is false

removeEventHandler('onClientPlayerDamage',target,updateMyTarget) 

then the function would be

function updateMyTarget() 
            --getElementHealth(source) and other stuff to update 
end 

Link to comment

I don't quite understand what you mean, but I gave it a shot and it leaks like crazy.

function guiTargetInformation(target)    
    local updateTimer = 0 
    if (target) then 
        if (updateTimer = 0) then 
            updateTimer = setTimer(guiTargetInformation, 250, 0, target) 
        end 
        guiSetText(targetHealth, "Health:"..getElementHealth(target)) 
        guiSetVisible(targetWindow, true) 
    else 
        updateTimer = 0 
        killTimer(updateTimer) 
        guiSetVisible(targetWindow, false) 
    end 
end) 

Link to comment
local gtarget 
function guiTargetInformation(target)    
    if (target) then 
        if gtarget and gtarget ~= target then 
              removeEventHandler('onClientPlayerDamage',gtarget,updateMyTarget) 
        end 
        addEventHandler('onClientPlayerDamage',target,updateMyTarget) 
        guiSetText(targetHealth, "Health:"..getElementHealth(target)) 
        guiSetVisible(targetWindow, true) 
    else 
        if gtarget then 
              removeEventHandler('onClientPlayerDamage',gtarget,updateMyTarget) 
              gtarget = nil 
        end 
        killTimer(updateTimer) 
        guiSetVisible(targetWindow, false) 
    end 
end) 
  
function updateMyTarget() 
     guiSetText(targetHealth, "Health:"..getElementHealth(source)) 
end 

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