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)
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.
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.
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()
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.