[SOLVED] Fixing an overhead healthbar

Explanation

Hey guys! So recently i’ve come into an issue where the healthbar I made doesn’t seem to properly scale with the rest of the BillboardGUI, the entire gui uses scale over offset after learning that makes them go bonkers but doesn’t seem to fix the actual health bar.

I tried switching out the health bars UI with an ImageLabel and regular Frame, would I need to use a specific type of UI in order to keep it at the same size?

Some examples of what I mean are depicted below but if anything you feel i’d need to supply to fix this please ask.

Photos

image



image
image

your LocalScript is confusing me a bit. what exactly are the update delay loops for? and couldn’t you just use a ServerScript to handle every player’s HealthBar?

if you have a Bar and a Fill frame, your Fill should be sized with CurrentHealth / Maxhealth

Bar.Fill.Size = UDim2.fromScale(Humanoid.Health / Humanoid.MaxHealth, 1)

{9386688E-5960-4DDC-A684-9038AD3BE9C4}

1 Like

I am not personally a programmer, my friend helped write the script. Could it very well be the script itself messing with the nametag?

Heres a file of the system in case you’d like to take a deeper look.
OverheadUI.rbxl (72.5 KB)

Its applying an offset to the Y position as seen in the code

local newFrameSize = UDim2.new(0, CurrentHealth * 3, 0, 8)

UDim2 uses four components being: XScale, XOffset, YScale, and YOffset
Scale is the percentage that the UI will take up according to what its parented to. Such as this health bar’s parent Frame
Offset is based on pixels, which means that its size and position is based on your screen resolution.
YOffset is applying a offset of 8, meaning that it will move downwards 8.


Based on the size, its using only the Offset portion of the bar is the only thing being updated, meaning that will will always have a fixed sized based on how big the UI is, in order to fix, its going to need to be switched to Scale.
I dont expect you to know how to adjust the UI properly (I have trouble sometimes) so here’s the finished result when switching the bar from Offset to Scale, and some updates to the hirearchy, and in order to script the bar accordingly, use what @BonesIsUseless reccommended:

--// Health/MaxHealth is the percentage that the health is that
-- when applied to XScale, it will cover based on the percentage given
-- (50% = 0.5) will cover 50% of the bar

--// The 1 on YScale is so the Height of the bar always covers the height
-- unless you are trying to add a second bar, dont mess with it.

--// 'Bar' is the outline of the healthbar, and or Back portion
-- its responsible for the sizing of the bar
--// 'Fill' is the thing you want to update, and will update
-- accordingly to the sizer of 'Bar'

If you use Offset instead of Scale, it will not adjust properly.

OverheadUI.rbxl (72.6 KB)

Also, tell your friend that instead of updating every second, he should update when the health of the player updates using Humanoid.HealthChanged, this would much less expensive on the systems end.

1 Like

Thank you so much to both of you, i’ll be sure to let him know and take notes for myself :slight_smile:

well your friend made a LocalScript (which would only update the healthbar on your end; nobody else would see it update)

here’s how i would do it with a ServerScript

local Players = game.Players
local TweenService = game:GetService("TweenService")
local tInfo = TweenInfo.new(0.15, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ChangeTitleEvent = ReplicatedStorage.Events.ChangeTitle

-- I would probably store the player's nametag info incase the player dies or rejoins
ChangeTitleEvent.OnServerEvent:Connect(function(Player, GroupID, Title, Color)
  if not Player.Character then return end

  local NametagGui = Player.Character.Head:FindFirstChild("NametagGui")
  if not NametagGui then warn("Failed to modify", Player.Name .. "'s", "NametagGui. NametagGui does not exist.") return end -- I don't see why the NameTagGui wouldn't exist

  NametagGui.GroupName.Text = Title
  NametagGui.GroupName.TextColor3 = Color
  NametagGui["Rank&Name"].Text = Player:GetRoleInGroup(GroupID).. ", " .. Player.Name
end)

-- Function to update a character's healthbar NewHealth is a scale value
local function UpdateCharacterHealthBar(Character, NewHealth)
  local NametagGui = Character.Head:FindFirstChild("NametagGui")
  if not NametagGui then warn("Failed to modify", Character.Name .. "'s", "NametagGui. NametagGui does not exist.") return end -- I still don't see why the NameTagGui wouldn't exist

  local HealthBar = NametagGui.HealthBar
  local Fill = HealthBar.Fill

  TweenService:Create(Fill, tInfo, {["Size"] = UDim2.fromScale(NewHealth, 1)}):Play()
end

-- Give each player a nametag when they join
Players.PlayerAdded:Connect(function(Player)
  Player.CharacterAdded:Connect(function(Character)
    -- Give the player a Nametag if they don't have one
    local NametagGui = Character.Head:FindFirstChild("NametagGui") or script:WaitForChild("NametagGui"):Clone()
    NametagGui.Parent = Character.Head

    local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
    UpdateCharacterHealthBar(Character, 1)

    Humanoid.HealthChanged:Connect(function(NewHealth)
      UpdateCharacterHealthBar(Character, NewHealth / Humanoid.MaxHealth)
    end)
  end)
end)
1 Like