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)