Jump to content

Health Decrease/Increase Animation


Recommended Posts

Hey MTA Community, i was trying to make a rectangle which shows player health and it should decrease/increase slowly when u lose/earn health, but it only increases when the script starts

Code:

function updateStuff()
theHealth = getElementHealth(localPlayer)
end
addEventHandler("onClientRender",root,updateStuff)

local tick = getTickCount()/5

addEventHandler("onClientRender", root, 
    function() 
        local progress = ((getTickCount()/5 - tick)/1000) 
        local cX, cY = interpolateBetween(0, 0, 0, 0, theHealth, 0, progress, "Linear")
        dxDrawRectangle(450, 0, 100 * 4, 30, tocolor(0,0,0,150)) 
        dxDrawRectangle(450, 0, cY * 4, 30, tocolor(100,0,0,255)) 
    end 
)

 

Link to comment
  • Discord Moderators
local tick, healthLastFrame, animateTo = 0, 0

addEventHandler("onClientRender", root, 
    function() 
        local health = getElementHealth(localPlayer)
        if (healthLastFrame~=health) then 
            tick = getTickCount() 
            animateTo = health
        else
            healthLastFrame = health
        end
        local progress = (getTickCount()-tick)/1000
        dxDrawRectangle(450, 0, 400, 30, tocolor(0, 0, 0, 150)) 
        dxDrawRectangle(450, 0, 400*animateTo*((progress>1 and 1) or progress)/100, 30, tocolor(100, 0, 0, 255)) 
    end 
)

Should work.
'Should' bc i haven't tried it.

Link to comment
savedH,startedTick = 0,false

addEventHandler("onClientRender", root,function()
	if ( savedH < getElementHealth(localPlayer) or savedH > getElementHealth(localPlayer) ) then
		if not( startedTick ) then
			tick = getTickCount()/5
			startedTick = true
		end
	else
		startedTick = false
	end
	local progress = ((getTickCount()/5 - tick)/1000) 
	local cX, cY = interpolateBetween(0, savedH, 0, 0, getElementHealth(localPlayer), 0, progress, "Linear")
	dxDrawRectangle(450, 0, 100 * 4, 30, tocolor(0,0,0,150)) 
	dxDrawRectangle(450, 0, cY * 4, 30, tocolor(100,0,0,255)) 
	savedH = cY
end)

 

Edited by TheOtherSide
Link to comment
On 7/22/2018 at 19:03, TheOtherSide said:

savedH,startedTick = 0,false

addEventHandler("onClientRender", root,function()
	if ( savedH < getElementHealth(localPlayer) or savedH > getElementHealth(localPlayer) ) then
		if not( startedTick ) then
			tick = getTickCount()/5
			startedTick = true
		end
	else
		startedTick = false
	end
	local progress = ((getTickCount()/5 - tick)/1000) 
	local cX, cY = interpolateBetween(0, savedH, 0, 0, getElementHealth(localPlayer), 0, progress, "Linear")
	dxDrawRectangle(450, 0, 100 * 4, 30, tocolor(0,0,0,150)) 
	dxDrawRectangle(450, 0, cY * 4, 30, tocolor(100,0,0,255)) 
	savedH = cY
end)

 

gg ^_^

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