Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Adjusting custom health bar?

I made a custom health GUI bar and I have a LocalScript in it for it to change it's size depending on when a particular player gets damaged. The issue I have is the bar is extending the actual GUI I made for it and the percentages are way off. Could someone help me? The player has around 5,000 health as opposed to 100.

As you can see in this picture, http://imgur.com/sMEoPxy

the lime green bar is NOT part of what I made in the GUI, I think it is part of my LocalScript. This is what my LocalScript looks like

script:WaitForChild("RemoteEvent")

script.RemoteEvent.OnClientEvent:connect(function()
	for i, v in pairs(game.Players:GetChildren()) do
		if v.TeamColor == BrickColor.new("Bright red") then	
		local bosshealth = v.Character.Humanoid.Health
		local bossmaxhealth = v.Character.Humanoid.MaxHealth
		script.Parent.Size = UDim2.new(bosshealth / bossmaxhealth,0,0,20)		
		end
	end
end)

Please do not worry about why I am using a remoteevent and why I am doing for i, etc. I know why and I THINK it is fine I am just worried about the health bar.

1 comment

0

0,0,20???

1 answer

✓ Accepted answer

The problem here is that you are not doing the equation properly. For example, if you are creating a health bar, and are sizing the actual bar using it's scale property (which is usually best), you use the following equation:

1/maxHealth*currentHealth

1 being the 'full' size of the bar.

The point of this equation is to divide the bar into segments (if a Humanoid has a MaxHealth of 3, it returns 1/3), then you multiply it by the current health someone has.

If a Humanoid has 1 Health, it will make the bar 1/3 of the maximum size. If a player has 2 Health, it will make it 2/3 the maximum size, and so on.


The other problem is that your GUI object is not utilizing scale properly.

It does not have to be using scale, but it is recommended for simplicity. To use the scale property properly, the bar needs to be inside a 'holder' that is the full size. Also note that the recommended maximum size of an object using scale is 1. Any more, and the object is bigger than it's parent. This is also what you are doing wrong with the equation, as MaxHealth is bigger than 1.

Here is an explorer example:

  • Gui
    • HealthBar (size = 0,100,0,10 - 100 pixels wide and 10 pixels tall)
      • Bar (size = 1,0,1,0 - the bar's parent's x and y sizes)
        • LocalScript

Updated code:

-- ill leave the rest to be implemented by you
function updateSize()
	script.Parent.Size = UDim2.new(1/maxHealth*currentHealth,0,1,0)
end

Hope I helped!

~TDP

This discussion is preserved as it was. New answers, comments, and votes are closed.