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)
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
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
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)
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)
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?