Health GUI getting too big when changing health

PROBLEM:


WHAT IS SUPPOSED TO LOOK LIKE:

SCRIPT:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid:Humanoid = character:FindFirstChild("Humanoid")

runservice.RenderStepped:Connect(function()
	script.Parent:TweenSize(UDim2.new(humanoid.Health/humanoid.MaxHealth, 0, 1, 0), "Out", "Linear", 0.5, false)
end)
2 Likes

I have tried many other possible solutions from different topics like this but things just don’t work.

is the light grey bar parented to the dark grey bar?

Is the GUI size 1, 0, 1, 0? Not on the script, but the grey bar itself

it is shown on the script that he uses renderstepped to constantly tween and resize the gui, so yes

Gotcha. So a simple fix is to just put Grey Bar Frame in a different frame with that same size, then set the Grey Bar Size to 1,0,1,0

my guess is that he probably doesn’t have the light grey bar parented to the dark grey. in the real game you can still see the grey bar on the edge

1 Like

Try using SCALE instead of OFFSET

I somehow managed to fix something, now it is not getting bigger. But one problem though how can I prevent the health gui from exceeding to its background.

image

1 Like

the script should automatically lock the bar in place… weird

i suggest you check the gui while playtesting, is it still 1,0,1,0 in size while looking like that?

if a script is interacting with it, then make it so that each heartbeat second the size will stay the same: and you dont have a seperate bar that indicates their health…

local runservice = game:GetService("RunService")
local XAxis = {Scale = 1, Offset = 0} -- set the variables.
local YAxis = {Scale = 0.1, Offset = 0} -- set this too.
local element = script.Parent -- the grey frame.
runservice.RenderStepped:Connect(function()
     element.Size = UDim2.new(XAxis.Scale, XAxis.Offset, YAxis.Scale, YAxis.Offset)
end)

elsewhere if it’s only being like that naturally without any script changing the size, then you should probably resize it a bit smaller from the X-axis. otherwise, increase the really dark-grey colored frame.

i’m not sure why you are using strings to define the Scale and Offset.

Looks like the moving health bar isn’t inside a frame set to the desired size. In my game, I have the frame for the health bar (Health) sized like I want then have another frame (MovingHealthBar) inside it. I can then resize MovingHealthBar by (Health / MaxHealth,0,1,0) without anything extra.

Also, running a tween every frame is wildly inefficient. Edits to the moving bar’s size should only be done when the humanoid’s Health and/or MaxHealth properties ACTUALLY change. Fortunately, you can watch for changes easily with :GetPropertyChangedSignal().

local Dead = false
local function UpdateHealthbar()
	if Dead then return end
	local health = math.clamp(Humanoid.Health / Humanoid.MaxHealth,0,1)
	ts:Create(script.Parent,TweenInfo.new(0.5,Enum.EasingStyle.Linear),{Size = UDim2.new(health,0,1,0)}):Play()
end

humanoid:GetPropertyChangedSignal("Health"):Connect(UpdateHealthbar)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(UpdateHealthbar)
humanoid.Died:Connect(function()
	humanoid.Health = 0
	UpdateHealthbar()
	Dead = true
end)

UpdateHealthbar()

There’s an error in your script, once someone dies the variable “Dead” is always gonna be true, and the person is using a frame and not something parented to their humanoid or head.

-- removed the "Dead" variable cause it's entirely useless.
local ts = game:GetService("TweenService")

local function UpdateHealthbar()
    -- removed "returning"
	local health = math.clamp(Humanoid.Health / Humanoid.MaxHealth,0,1)
	ts:Create(script.Parent,TweenInfo.new(0.5,Enum.EasingStyle.Linear),{Size = UDim2.new(health,0,1,0)}):Play()
end

local function DeadHealthBar() -- seperate function for dying.
   	ts:Create(script.Parent,TweenInfo.new(0.5,Enum.EasingStyle.Linear),{Size = UDim2.new(0,0,1,0)}):Play()
end

humanoid.Died:Connect(DeadHealthBar)

humanoid:GetPropertyChangedSignal("Health"):Connect(UpdateHealthbar)
humanoid:GetPropertyChangedSignal("MaxHealth"):Connect(UpdateHealthbar)


UpdateHealthbar()

this should fix it.

I wrote my script with the assumption that the ScreenGui’s ResetOnSpawn property is set to true, which is how I have it in my game because accounting for respawns with code is a pain.

That’s reasonable,
i dont see no reason to use ResetOnSpawn property

but i think it’s a better practice to just keep the property off instead of on IF
it’s releated to a gui without it being parented to a humanoid or a head.

otherwise you can keep the property on