✓ Accepted answer
That's because you are setting X scale of the Health bar to PlayerHealth variable, if player has 100 health, it will be 100 times larger than object that this bar is parented to, you must get max health of the player and in some cases (not in yours if i am sure) default size of the health bar to make it work.
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild('Humanoid')
local Frame = script.Parent
local HealthText = Frame.Parent.Parent.HealthLabel
local function UpdateHealth()
local PlayerHealth = Humanoid.Health
Frame:TweenSize(
UDim2.fromScale(PlayerHealth / Humanoid.MaxHealth, 1),
Enum.EasingDirection.Out, Enum.EasingStyle.Quad,
0.15
)
HealthText.Text = "Health: " .. PlayerHealth
end
Humanoid:GetPropertyChangedSignal('Health'):Connect(UpdateHealth)
I assume your HealthText variable has default size 1, 0, 1, 0, in this case all you do is divide humanoid max health with humanoid current health. I used GetPropertyChangedSignal which returns an event which fires every time given property of the instance changes, in this case it's health, you could use HealthChanged but it does not fire if new health is higher than max health so i am not sure if it would be the best option for a health bar. In your case you used RunService which you should not because it just eats the memory for no reason and fires the function for no reason.
You also used wait at the start i assume to wait for the character to load? Don't do that, it won't work, you also did not wait for the character, i used CharacterAdded event which fires when the character is added and added :Wait which is a function that waits until given event fires and returns what the event returns, that way you can be sure that the character exists, next i used WaitForChild for the humanoid, it will yield until humanoid is found so no need for wait.
Sources
CharacterAdded
GetPropertyChangedSignal
Humanoid.HealthChanged
:Wait
WaitForChild
1 comment
That worked spectacularly. Thanks!