How do i make a boss health bar?

Hi, I am working on my rpg game, and i was wondering how i could make a boss health bar for all the humanoid models with the tag/attribute boss, someone could help me on how to check if the boss actually exists?, and well, should checking distance be a good method?, I also ran into a problem where when the boss respawns, the health bar doesn’t works anymore, this is the code/reference i am using :

local TweenService = game:GetService("TweenService")
local Stepped = game:GetService("RunService").Stepped
local Abb = require(game.ReplicatedStorage.ShortenNumber)
local player = game.Players.LocalPlayer
local bossHealthBarGui = player.PlayerGui:WaitForChild("BossHealthBarCopyGui")
local healthFrame = bossHealthBarGui.BossHealthBarFrame
local healthBar = healthFrame.HealthInfo.HealthBar

-- We don't need to create a clone of the boss health bar unless there will be multiple bosses at the same time

-- Keep track of boss components --
local boss = workspace:WaitForChild("Mobs").GigantiumGoblin
local bossHumanoid = boss.Enemy

local bossHealthChangedConnection = bossHumanoid.HealthChanged:Connect(function() --I want this script makes it so this gui only appears when player get near the boss or it will instantly disappear until player encounters an boss
	-- Code in here will run each time the Boss health changes --
	-- So, you want to update the Gui here (** in the code line 18 ~ 36, I put an boss health bar script code from the original game I am working on**) --
	bossHealthBarGui.Enabled = true
		local HBT = TweenService:Create(healthBar.Fill, TweenInfo.new(1.5/4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Size = UDim2.new(bossHumanoid.Health / bossHumanoid.MaxHealth,0,1,0)
	})HBT:Play()
	healthBar.Health.Text = Abb.Abb2(bossHumanoid.Health) .."/".. Abb.Abb2(bossHumanoid.MaxHealth)
	healthBar.Percentage.Text = math.floor(bossHumanoid.Health / bossHumanoid.MaxHealth * 100) .. "%"

	if bossHumanoid.Health <= bossHumanoid.MaxHealth * 0.33 then
		local CT1 = TweenService:Create(healthBar.Fill, TweenInfo.new(3/4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {BackgroundColor3 = script.Low.Value
		})CT1:Play()

	elseif bossHumanoid.Health <= bossHumanoid.MaxHealth * 0.66 and bossHumanoid.Health >= bossHumanoid.MaxHealth * 0.33 then
		local CT2 = TweenService:Create(healthBar.Fill, TweenInfo.new(3/4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {BackgroundColor3 = script.Medium.Value
		})CT2:Play()
	else
		local CT3 = TweenService:Create(healthBar.Fill, TweenInfo.new(3/4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {BackgroundColor3 = script.High.Value
		})CT3:Play()
	end
end)

-- The code inside here will run when the Boss dies --
bossHumanoid.Died:Once(function()
	bossHealthChangedConnection:Disconnect() -- Disconnect event we don't need anymore --
	bossHealthBarGui.Enabled = false -- Hide the Health Bar when the Boss dies --
	print("Boss has Died!")
end)
1 Like

Hello.

So, first you have to insert a tag named “BossHumanoid” to every humanoid of boss.
Now, the health bar upon respawning probably does not work because you disconnected it in the Died function.

--// Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")

--// Modules
local Abb = require(ReplicatedStorage.ShortenNumber)

--// Player Elements
local Player = Players.LocalPlayer

--// GUI Elements
local BossHealthBarGui = Player.PlayerGui:WaitForChild("BossHealthBarCopyGui")
local HealthFrame = BossHealthBarGui.BossHealthBarFrame
local HealthBar = HealthFrame.HealthInfo.HealthBar

local BossHumanoid: Humanoid = CollectionService:GetTagged("BossHumanoid") -- Make sure every boss Humanoid has a tag "BossHumanoid".

BossHumanoid.HealthChanged:Connect(function() --I want this script makes it so this gui only appears when player get near the boss or it will instantly disappear until player encounters an boss
    -- Code in here will run each time the Boss health changes --
    -- So, you want to update the Gui here (** in the code line 18 ~ 36, I put an boss health bar script code from the original game I am working on**) --
    BossHealthBarGui.Enabled = true
    local HBT = TweenService:Create(HealthBar.Fill, TweenInfo.new(1.5/4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {Size = UDim2.new(BossHumanoid.Health / BossHumanoid.MaxHealth,0,1,0)
    })HBT:Play()
    HealthBar.Health.Text = Abb.Abb2(BossHumanoid.Health) .."/".. Abb.Abb2(BossHumanoid.MaxHealth)
    HealthBar.Percentage.Text = math.floor(BossHumanoid.Health / BossHumanoid.MaxHealth * 100) .. "%"

    if BossHumanoid.Health <= BossHumanoid.MaxHealth * 0.33 then
        local CT1 = TweenService:Create(HealthBar.Fill, TweenInfo.new(3/4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {BackgroundColor3 = script.Low.Value
        })CT1:Play()

    elseif BossHumanoid.Health <= BossHumanoid.MaxHealth * 0.66 and BossHumanoid.Health >= BossHumanoid.MaxHealth * 0.33 then
        local CT2 = TweenService:Create(HealthBar.Fill, TweenInfo.new(3/4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {BackgroundColor3 = script.Medium.Value
        })CT2:Play()
    else
        local CT3 = TweenService:Create(HealthBar.Fill, TweenInfo.new(3/4, Enum.EasingStyle.Quint, Enum.EasingDirection.Out), {BackgroundColor3 = script.High.Value
        })CT3:Play()
    end
end)

-- The code inside here will run when the Boss dies --
BossHumanoid.Died:Once(function()
    BossHealthBarGui.Enabled = false -- Hide the Health Bar when the Boss dies --
    print("Boss has Died!")
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.