I making a healthbar and i made a health script, but the health keeps extending
How do i fix this?
wait(0.2)
while true do
local hp = game.Players.LocalPlayer.Character.Humanoid.Health/250
script.Parent.Parent.Indicator.Text = game.Players.LocalPlayer.Character.Humanoid.Health.. "/"..game.Players.LocalPlayer.Character.Humanoid.MaxHealth
script.Parent:TweenSize((UDim2.new(hp,0,0.6,0)),Enum.EasingDirection.In,Enum.EasingStyle.Quad,0.15)
wait(0.2)
end
Your bar is extending because the power is too high, set the power to more lower until it fits, and in this case, if you want it to still be 250, you will have to deal with extending all of it
Firstly, I think you’re better using the HealthChanged event of the Humanoid instead so it only calls the code when they have been damaged, rather then every fifth of a second. Another thing you should do to fix this is to make the gui update with respect to the original X scale of the GUi at max hp
Try This
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local frame = script.Parent
local indicatorText = frame.Parent.Indicator
local xSize = frame.Size.X.Scale --Getting the original size
hum.HealthChanged:Connect(function(newhp)
local actualhp = math.floor(newhp) --Doing this since the hp given is a long decimal stream
local newSize = (newhp/hum.MaxHealth) * xSize
indicatorText.Text = newhp.. "/"..hum.MaxHealth
frame:TweenSize((UDim2.new(newSize,0,0.6,0)),Enum.EasingDirection.In,Enum.EasingStyle.Quad,0.15,true)
wait(0.2)
end)
Edit: Fixed the code for an issue that most likely is gonna appear about indicatorText