+Health Text System

Hey Devforum,
I am working on a health bar system for my game but there is one problem everything works fine but I want to know how to make a text come up saying +5 for my health or -5 if I take damage
Screenshot 2023-08-22 at 5.20.39 PM
like this.
Screenshot 2023-08-22 at 5.23.42 PM

1 Like

you could do something like this

	local oldHealth = 100
	local plr = game.Players.LocalPlayer
	local char = plr.Character
	local humanoid = char:FindFirstChild("Humanoid")

	humanoid.GetPropertyChangedSignal('Health'):Connect(function(health)
		if oldHealth >= health then
			--change the ui to be green and display the health diff
		elseif oldHealth <= health then
			--change the ui to be red and display the health diff
	end
	oldHealth = health
	end)

not sure if this would work/if this is what your even looking for

1 Like

it could work i got to look through it. but what I am trying to do to give you context is make a system where if I take damage it displays how much damage i took and if I gain health it shows how much health I gained.

1 Like

yeah, thats what that does, you just have to make the ui/set the ui’s textcolor to wtv (as said by the comment in the script)

1 Like

alright let me try it out in studio

1 Like
local plr = game.Players.LocalPlayer
local char = plr.Character

local screenGui = script.Parent
local greenTextLabel = screenGui.Green
local redTextLabel = screenGui.Red

-- Make sure the character exists before accessing the humanoid
if char then
	local humanoid = char:FindFirstChild("Humanoid")

	if humanoid then
		humanoid:GetPropertyChangedSignal('Health'):Connect(function()
			local health = humanoid.Health
			local healthDiff = oldHealth - health

			if healthDiff > 0 then
				-- Decrease in health, show red text
				redTextLabel.Text = "-" .. tostring(healthDiff)
				redTextLabel.Visible = true
				greenTextLabel.Visible = false -- Hide green text
			elseif healthDiff < 0 then
				-- Increase in health, show green text
				greenTextLabel.Text = "+" .. tostring(math.abs(healthDiff))
				greenTextLabel.Visible = true
				redTextLabel.Visible = false -- Hide red text
			end

			oldHealth = health
		end)
	end
end

This is what I have so far but its not working properly, the text doesnt appear.

1 Like

my guess is because you never defined oldHealth until the bottom of the script, so the top of the script got nil

1 Like

This stops working if the character doesn’t immediately exist or if the character dies/resets.

player.CharacterAdded:Connect(function(char)
    -- all the character code from @DemonicPandazYT
end)

Edit: in practice, I’d do this:

local function OnChar(char)
local humanoid = char:FindFirstChild("Humanoid")

	if humanoid then
		humanoid:GetPropertyChangedSignal('Health'):Connect(function()
			local health = humanoid.Health
			local healthDiff = oldHealth - health

			if healthDiff > 0 then
				-- Decrease in health, show red text
				redTextLabel.Text = "-" .. tostring(healthDiff)
				redTextLabel.Visible = true
				greenTextLabel.Visible = false -- Hide green text
			elseif healthDiff < 0 then
				-- Increase in health, show green text
				greenTextLabel.Text = "+" .. tostring(math.abs(healthDiff))
				greenTextLabel.Visible = true
				redTextLabel.Visible = false -- Hide red text
			end

			oldHealth = health
		end)
	end
end

if plr.Character then OnChar(plr.Character) end

plr.CharacterAdded:Connect(OnChar)
1 Like

i was able to modify that script and this is what i have

local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local indicatorFrame = script.Parent -- Reference to the Frame for indicators

local previousHealth = humanoid.Health
local maxIndicators = 4 -- Maximum number of indicators on screen
local indicatorList = {} -- Store indicator instances

local cooldown = false
local cooldownTime = 2 -- Cooldown time in seconds

local function createIndicator(amount, isDamage)
	local indicator = Instance.new("TextLabel")
	indicator.Size = UDim2.new(0, 100, 0, 20)
	indicator.Position = UDim2.new(0, math.random(0, indicatorFrame.AbsoluteSize.X - 100), 0, math.random(0, indicatorFrame.AbsoluteSize.Y - 20))
	indicator.BackgroundTransparency = 1
	indicator.TextScaled = true
	indicator.Text = (isDamage and "-" or "+") .. tostring(amount)
	indicator.TextColor3 = isDamage and Color3.fromRGB(255, 0, 0) or Color3.fromRGB(0, 255, 0) -- Red or Green color
	indicator.Font = Enum.Font.LuckiestGuy
	indicator.TextStrokeTransparency = 0 -- Set text stroke transparency to 0
	indicator.Parent = indicatorFrame

	table.insert(indicatorList, indicator)

	wait(2) -- Display the indicator for 2 seconds

	indicator:Destroy()
	table.remove(indicatorList, 1) -- Remove the indicator from the list
end

local function onHealthChanged()
	local healthDifference = humanoid.Health - previousHealth
	if healthDifference < 0 then
		print("Creating damage indicator:", math.abs(healthDifference))
		local numIndicators = math.ceil(math.abs(healthDifference) / 5) -- Calculate the number of damage indicators to display
		for i = 1, numIndicators do
			createIndicator(5, true) -- Display damage indicators
		end
	elseif healthDifference > 0 then
		if not cooldown then
			local numIndicators = math.ceil(healthDifference / 5) -- Calculate the number of healing indicators to display
			print("Creating", numIndicators, "healing indicator(s)")
			for i = 1, numIndicators do
				createIndicator(5, false) -- Display healing indicators
			end
			cooldown = true
			wait(cooldownTime)
			cooldown = false
		else
			wait(cooldownTime) -- Wait for the existing cooldown
		end
	end
	previousHealth = humanoid.Health
end


humanoid.HealthChanged:Connect(onHealthChanged)

but there is one problem, the health stacks for example when i eat a frut that has 3 clicks it says +5 +10 +15 and when i take damage its the same

1 Like