JeViCo Posted February 1, 2018 Share Posted February 1, 2018 (edited) Hello again I'm making some kind of HUD and i want to know, how to make health bar move backwards? local sx,sy = guiGetScreenSize() local px,py = 1280,720 local x,y = (sx/px), (sy/py) dxDrawLine(x*447 - 1, y*615 - 1, x*447 - 1, y*636, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*682, y*615 - 1, x*447 - 1, y*615 - 1, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*447 - 1, y*636, x*682, y*636, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*682, y*636, x*682, y*615 - 1, tocolor(0, 0, 0, 255), 1, false) dxDrawRectangle(x*447, y*615, x*235/100*health, y*21, tocolor(255, 0, 0, 255), false) --health i think i'm close but still can't figure it out Edited February 1, 2018 by JeViCo Link to comment
NeXuS™ Posted February 1, 2018 Share Posted February 1, 2018 (edited) What do you mean "moving backwards"? Edited February 1, 2018 by NeXuS™ 1 Link to comment
#RooTs Posted February 1, 2018 Share Posted February 1, 2018 x*235/+100*health or x*235/-100*health 1 Link to comment
NeXuS™ Posted February 1, 2018 Share Posted February 1, 2018 48 minutes ago, #RooTs said: x*235/+100*health or x*235/-100*health I don't think he is trying to achieve drawing it on the other side of the rectangle. Link to comment
ShayF2 Posted February 1, 2018 Share Posted February 1, 2018 I posted code that may help here, please do not make multiple threads here. Link to comment
itHyperoX Posted February 1, 2018 Share Posted February 1, 2018 (edited) use this dxDrawRectangleBox(x*447, y*615, x*235/100, y*21, math.floor(getElementHealth(localPlayer)), tocolor(0, 0, 0, 150)) function dxDrawRectangleBox(startX, startY, sizeX, sizeY, playerStat, color) dxDrawRectangle(startX, startY, sizeX, sizeY, tocolor(107, 107, 107, 180)) --box dxDrawRectangle(startX-2, startY-2, 2, sizeY+4, tocolor(0, 0, 0, 220)) --left dxDrawRectangle(startX+sizeX, startY-2, 2, sizeY+4, tocolor(0, 0, 0, 220)) --right dxDrawRectangle(startX, startY-2, sizeX, 2, tocolor(0, 0, 0, 220)) --up dxDrawRectangle(startX, startY+sizeY, sizeX, 2, tocolor(0, 0, 0, 220)) --down dxDrawRectangle(startX, startY, sizeX/100*playerStat, sizeY, color) --stat end Edited February 1, 2018 by TheMOG Link to comment
ShayF2 Posted February 1, 2018 Share Posted February 1, 2018 (edited) 12 minutes ago, TheMOG said: use this dxDrawRectangleBox(x*447, y*615, x*235/100, y*21, math.floor(getElementHealth(localPlayer)), tocolor(0, 0, 0, 150)) function dxDrawRectangleBox(startX, startY, sizeX, sizeY, playerStat, color) dxDrawRectangle(startX, startY, sizeX, sizeY, tocolor(107, 107, 107, 180)) --box dxDrawRectangle(startX-2, startY-2, 2, sizeY+4, tocolor(0, 0, 0, 220)) --left dxDrawRectangle(startX+sizeX, startY-2, 2, sizeY+4, tocolor(0, 0, 0, 220)) --right dxDrawRectangle(startX, startY-2, sizeX, 2, tocolor(0, 0, 0, 220)) --up dxDrawRectangle(startX, startY+sizeY, sizeX, 2, tocolor(0, 0, 0, 220)) --down dxDrawRectangle(startX, startY, sizeX/100*playerStat, sizeY, color) --stat end My code for his other thread that I posted above has everything you'll ever need. If he wants to draw lines around it then he can. My code is Auto relative, he puts in his resolution then positions it and sizes it, it will resize and reposition for other resolutions. He can also change the colors of it. Mainly the progress of the progress bar, which is controlled when player loses health. styles = {} styles['progressBar'] = { ['default'] = { bgColor = tocolor(0,0,0,180), barColor = tocolor(255,255,255,200), textColor = tocolor(255,0,0,255) } } local sx,sy = guiGetScreenSize() local sw,sh = 1360,768-- Change this to your resolution and use absolute values when creating elements. local draw = {} local validTypes = {'progressBar'} function dxRect(...) arg[1],arg[2],arg[3],arg[4] = arg[1]/sw*sx,arg[2]/sh*sy,arg[3]/sw*sx,arg[4]/sh*sy return dxDrawRectangle(unpack(arg)) end function dxText(...) arg[2],arg[3],arg[4],arg[5],arg[7] = arg[2]/sw*sx,arg[3]/sh*sy,(arg[4]+arg[2])/sw*sx,(arg[5]+arg[3])/sh*sy,(arg[7] or 1)/sw*sx return dxDrawText(unpack(arg)) end function dx(type,theme,x,y,w,h,visible) for i=1,#validTypes do if type == validTypes[i] then local self = {} self.type = type self.x = x or 0 self.y = y or 0 self.w = w or 0 self.h = h or 0 self.style = styles[self.type][theme] or styles[self.type]['default'] self.visible = visible or true self.destroy = function() for i=1,#draw do if draw[i] == self then table.remove(draw,i) end end end return self end end return false end function dxCreateProgressBar(text,x,y,w,h) local self = dx('progressBar','default',x,y,w,h,true) if self then self.progress = 0 local pbText = text self.draw = function() dxRect(self.x,self.y,self.w,self.h,self.style.bgColor) if string.find(pbText,'%progress%',1,true) then pbText = string.gsub(pbText,'%progress%',tostring(math.floor(self.progress))..'%') end dxText(pbText,self.x,self.y,self.w,self.h,self.style.textColor) dxRect(self.x,self.y,(self.w/100)*self.progress,self.h,self.style.barColor) end table.insert(draw,self) return self end end addEventHandler('onClientRender',root,function() for i=1,#draw do if draw[i].visible then draw[i].draw() end end end) local progressBar = dxCreateProgressBar('Health %progress%',500,500,600,200)-- create a progress bar like this setTimer(function() local health = getPlayerHealth(localPlayer)-- if its 100 then use it, if its 200 then divide it by 2 if getPlayerMaxHealth(localPlayer) > 100 then health = health/2 end progressBar.progress = health -- then set the progress end,50,0) -- you can also set colors like this progressBar.style.barColor = tocolor(255,0,0,200) progressBar.style.bgColor = tocolor(255,255,255,160) Edited February 1, 2018 by ShayF Link to comment
DNL291 Posted February 1, 2018 Share Posted February 1, 2018 (edited) Try this: local sx,sy = guiGetScreenSize() local px,py = 1280,720 local x,y = (sx/px), (sy/py) local width = 235 local healthBarW = ( health / getPedMaxHealth() ) * width dxDrawLine(x*447 - 1, y*615 - 1, x*447 - 1, y*636, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*682, y*615 - 1, x*447 - 1, y*615 - 1, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*447 - 1, y*636, x*682, y*636, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*682, y*636, x*682, y*615 - 1, tocolor(0, 0, 0, 255), 1, false) dxDrawRectangle(x*447, y*615, healthBarW, y*21, tocolor(255, 0, 0, 255), false) --health function getPedMaxHealth() assert(isElement(localPlayer) and (getElementType(localPlayer) == "ped" or getElementType(localPlayer) == "player"), "Bad argument @ 'getPedMaxHealth' [Expected ped/player at argument 1, got " .. tostring(localPlayer) .. "]") local stat = getPedStat(localPlayer, 24) local maxhealth = 100 + (stat - 569) / 4.31 return math.max(1, maxhealth) end Edited February 1, 2018 by DNL291 added getPedMaxHealth 1 Link to comment
JeViCo Posted February 2, 2018 Author Share Posted February 2, 2018 (edited) 15 hours ago, DNL291 said: Try this: local sx,sy = guiGetScreenSize() local px,py = 1280,720 local x,y = (sx/px), (sy/py) local width = 235 local healthBarW = ( health / getPedMaxHealth() ) * width dxDrawLine(x*447 - 1, y*615 - 1, x*447 - 1, y*636, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*682, y*615 - 1, x*447 - 1, y*615 - 1, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*447 - 1, y*636, x*682, y*636, tocolor(0, 0, 0, 255), 1, false) dxDrawLine(x*682, y*636, x*682, y*615 - 1, tocolor(0, 0, 0, 255), 1, false) dxDrawRectangle(x*447, y*615, healthBarW, y*21, tocolor(255, 0, 0, 255), false) --health function getPedMaxHealth() assert(isElement(localPlayer) and (getElementType(localPlayer) == "ped" or getElementType(localPlayer) == "player"), "Bad argument @ 'getPedMaxHealth' [Expected ped/player at argument 1, got " .. tostring(localPlayer) .. "]") local stat = getPedStat(localPlayer, 24) local maxhealth = 100 + (stat - 569) / 4.31 return math.max(1, maxhealth) end I tried to remake it and it worked. I copied health bar more to the right, removed old one and made (-healh) on coordinates. Thank you) 17 hours ago, #RooTs said: x*235/+100*health or x*235/-100*health i was trying to manage with @DNL291's script until i opened wiki of dxDrawRectangle. It said that i use at least first coordinate + width + height. @#RooTs suggested to draw health bar backwards. Why not? Thanks everyone! Edited February 2, 2018 by JeViCo Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now