How can I fix this health display GUI

Hey !
I tried to make a health which resize depending on the Health / MaxHealth value of the character, but it doesnt resize correctly : here are the script and the screen

local health = game.ReplicatedStorage.health
local player = game.Players.LocalPlayer
local maxHealth = player.Character:WaitForChild("Humanoid").MaxHealth
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local humanoid = player.Character:WaitForChild("Humanoid")
script.Parent.BackgroundColor3 = player.Team.TeamColor.Color

health.Changed:Connect(function()
	local h = player.Character:WaitForChild("Humanoid").MaxHealth
	script.Parent.Size = UDim2.new( health.Value / maxHealth,0,1,0)
	script.Parent.Parent.HealthDisplay.Text = 'Health : '..health.Value
end)
2 Likes

Since it appears that the Healthbar is contained within another frame, the same solution I provided during the previous iteration of this topic should work correctly:

The scale value on the X Axis of the HealthBar would need to be multiplied by its default width so that when it ends up reaching something such as 50%, it would be 50% of the space that you allocated for it and not 50% of 1.

Example:

--[[ *Variable names here were changed from the post referenced below to align
with the new contents of this topic --]]
local percent = health.Value / maxHealth * 0.794 -- Number to replace

--[[

0.794 represents the size of the HealthBar's X Axis (in scale) when at MaxHealth

This means that a scale of 0.397 will have exactly half of the Healthbar drained
instead of 0.5 where it'd be around 62-63% when considering the default scale

--]]

script.Parent.Size = UDim2.new(percent, 0, 1, 0)
local health = game.ReplicatedStorage.health
local player = game.Players.LocalPlayer
local maxHealth = player.Character:WaitForChild("Humanoid").MaxHealth
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local humanoid = player.Character:WaitForChild("Humanoid")
script.Parent.BackgroundColor3 = player.Team.TeamColor.Color

local orginalSize = script.Parent.Size.X.Scale

health.Changed:Connect(function()
	local h = player.Character:WaitForChild("Humanoid").MaxHealth
	script.Parent.Size = UDim2.new( (health.Value / maxHealth) * originalSize,0,1,0)
	script.Parent.Parent.HealthDisplay.Text = 'Health : '..health.Value
end)

You need to get the original max size before you can scale

Basically as @StrongBigeMan9 provided but with an example of how it would be implemented in your script

1 Like

Should I set X = 100?
Id really understand what should be the x value

Imma try thanks : I didn’t see your message sorry ^^’

What the code does is set originalSize to the size the healthgui was first sized to as the max size the gui can be, then it will do your current health divided by your max health and then will multiply that by the original size to size it accordingly

1 Like

Thanks for explaining me ^^

1 Like

It underline this lne (* OriginalSize) part
script.Parent.Size = UDim2.new( (health.Value / maxHealth) * originalSize,0,1,0)

Wait what’s your code right now after changing

This one

local health = game.ReplicatedStorage.health
local player = game.Players.LocalPlayer
local maxHealth = player.Character:WaitForChild("Humanoid").MaxHealth
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local humanoid = player.Character:WaitForChild("Humanoid")
script.Parent.BackgroundColor3 = player.Team.TeamColor.Color

local orginalSize = script.Parent.Size.X.Scale

health.Changed:Connect(function()
	local h = player.Character:WaitForChild("Humanoid").MaxHealth
	script.Parent.Size = UDim2.new( (health.Value / maxHealth) * originalSize,0,1,0)
	script.Parent.Parent.HealthDisplay.Text = 'Health : '..health.Value
end)

You wrote the variable wrong, should be originalSize

Oh I didn’t even realise, I was focused on the health changed part

It still get out of the box
– r

It’s odd cuz it’s supposed to do, on max health : 175/175,0,1,0
Idk why it does that

That’s odd, I think it may be that’re not using the player’s heath but rather a value in replicatedStorage, maybe try

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local Teams = game:GetService("Teams")
script.Parent.BackgroundColor3 = player.Team.TeamColor.Color

local orginalSize = script.Parent.Size.X.Scale

humanoid.HealthChanged:Connect(function(hp)
	local usehp = math.floor(hp)
	script.Parent.Size = UDim2.new( (usehp / humanoid.MaxHealth) * originalSize,0,1,0)
	script.Parent.Parent.HealthDisplay.Text = 'Health : '..usehp
end)

Hmmmm I confess that using this method is odd…
Lemme try this

Now it doesn’t resize >:,) lmao

Oh wait, I forgot that you made a mistake in the variable and didn’t change that, I think you may’ve done the same again if you copied it, are you gettign any errors?

Edit: What about errors? are you getting any? This should work maybe do the maths separately? Also wait is the Gui scaled with Offset or Scale?

I changed it, don’t worry. One time, not 2

There’s a few things it could be then. Could you answer the questions I edited to my other reply?