Segmented Health Bar Not Working

I am trying to make a 5 segment health bar that matches player health in blocks of 20 to simulate having 5 hit points and flashes at low health. For some reason the script is completely non-functional and after looking on the Dev Hub and finding out that no one is making a health bar like I am, I’m pretty stumped. Below is the code and where it is in the File Explorer.

local player = game.Players.LocalPlayer
local humanoid = player.Character:WaitForChild("Humanoid")
local health = humanoid.Health
local bar100 = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("Full")
local bar80 = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("4HP")
local bar60 = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("3HP")
local bar40 = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("2HP")
local bar20red = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("1HPRED")
local bar20white = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("1HPWHITE")
local barempty = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("Empty")
bar100.Visible = true
bar80.Visible = false
bar60.Visible = false
bar40.Visible = false
bar20red.Visible = false
bar20white.Visible = false
barempty.Visible = false


local function changebar()
	if health > 80 then
		bar100.Visible = true
		bar80.Visible = false
	elseif health < 80 and health > 60 then
		bar100.Visible = false
		bar80.Visible = true
		bar60.Visible = false
	elseif health < 60 and health > 40 then
		bar80.Visible = false
		bar60.Visible = true
		bar40.Visible = false
	elseif health < 40 and health > 20 then
		bar60.Visible = false
		bar40.Visible = true
		bar20red.Visible = false
	elseif health < 20 and health > 0  then
		bar40.Visible = false
		bar20red.Visible = true
		bar20white.Visible = false
		while health < 20 and health > 0 do
			bar20white.Visible = true
			wait(0.5)
			bar20white.Visible = false
			wait(0.5)
		end
	else
		barempty.Visible = true
		bar20white.Visible = false
		bar20red.Visible = false
		bar40.Visible = false
		bar60.Visible = false
		bar80.Visible = false
		bar100.Visible = false
	end
end

humanoid:GetPropertyChangedSignal("Health"):Connect(changebar)

When you’re setting the health variable’s value, that variable’s value will be fixed unless you manually change it (it will not update even if the Humanoid’s health changes). Instead, get the real-time value when calling the changebar function.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local bar100 = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("Full")
local bar80 = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("4HP")
local bar60 = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("3HP")
local bar40 = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("2HP")
local bar20red = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("1HPRED")
local bar20white = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("1HPWHITE")
local barempty = game.StarterGui.GeneralGui.HealthFrame:WaitForChild("Empty")
bar100.Visible = true
bar80.Visible = false
bar60.Visible = false
bar40.Visible = false
bar20red.Visible = false
bar20white.Visible = false
barempty.Visible = false

local function changebar()
	local health = humanoid.Health

	if health > 80 then
		bar100.Visible = true
		bar80.Visible = false
	elseif health < 80 and health > 60 then
		bar100.Visible = false
		bar80.Visible = true
		bar60.Visible = false
	elseif health < 60 and health > 40 then
		bar80.Visible = false
		bar60.Visible = true
		bar40.Visible = false
	elseif health < 40 and health > 20 then
		bar60.Visible = false
		bar40.Visible = true
		bar20red.Visible = false
	elseif health < 20 and health > 0  then
		bar40.Visible = false
		bar20red.Visible = true
		bar20white.Visible = false
		while health < 20 and health > 0 do
			bar20white.Visible = true
			wait(0.5)
			bar20white.Visible = false
			wait(0.5)
		end
	else
		barempty.Visible = true
		bar20white.Visible = false
		bar20red.Visible = false
		bar40.Visible = false
		bar60.Visible = false
		bar80.Visible = false
		bar100.Visible = false
	end
end

Sorry for the late reply, but I tried it and it still does not work. Do I need to change the function call? Do you have any other suggestions? This is a very important part of my game.

I found the solution. The parameters for the health bar to change were incorrect. The health was at 80 but the script would only change if it was less then 80. That problem was throughout the entire script. All I had to do was add a “greater than or equal to” rather than using “greater than”. Thank you for your help though! ^^

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