I’m using math.clamp and dividing the current health by the max health. (e.g: my health is 100, max health is 100. 100/100 = 1). And I am using math.clamp with the minimum being 0, maximum being 1, keeping the bar from sizing or breaking.
your local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)
is outside your function which means that it isn’t updating in the update function.
Not the fix to the problem, but definitely a fix to address
local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local humanoid = chr:WaitForChild("Humanoid")
--/!!/ local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)
script.Parent.Parent:WaitForChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
local function update()
local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)
script.Parent.Parent:FindFirstChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
script.Parent:TweenSize(UDim2.fromScale(formula, 1), "InOut", "Linear", .5, false)
end
humanoid.HealthChanged:Connect(update)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)
Not the fix to the problem. However, it was an issue to address sooner or later.
Otherwise humanoid.Health/humanoid.MaxHealth will never be updated or changed to what humanoid.Health/humanoid.MaxHealth is if it’s not run in the function is what I meant.
Okay so, I figured everything out using the temp you gave.
Here is the working script. and I’ll explain below.
local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()
local humanoid = chr:WaitForChild("Humanoid")
script.Parent.Parent:WaitForChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
local sizeX = script.Parent.Size.X.Scale --/!!/
local sizeY = script.Parent.Size.Y.Scale --/!!/
local function update()
local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)
local finalX = sizeX * (formula) --/!!/
script.Parent.Parent:FindFirstChild("Percentage").Text = "Health: " .. humanoid.Health .. "%"
script.Parent:TweenSize(UDim2.fromScale(finalX , sizeY), "InOut", "Linear", .5, false)
end
humanoid.HealthChanged:Connect(update)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(update)
I may be awful at explaining this, as it is late while I’m typing this.
The reason it wasn’t working was due to using the X / Y scale of a GUI object is the % of what the scale of the screen is. For example what we were seeing is if you had 90% health, the tween is going to blow up the GUI object to 90% of the current X.Scale value of the screen.
So as soon I figured that out I just quickly got the X & Y scale themselves and took that and multiplied it by the % of health using you’re formula variable.
Side Note::
The sizeX & sizeY variables stay out of the function so they aren’t suppose to be updated every time the function is ran, otherwise you end up with undesired results if it calculates a new sizeX & Y every time updated is ran.
local formula = math.clamp(humanoid.Health/humanoid.MaxHealth, 0, 1)
This is just getting your current humanoid’s Health and MaxHealth properties when the script first executes and assigning the value returned by math.clamp(), if you want this ‘formula’ to be used dynamically then wrap it inside a function.