Problems With Health Display Script

Hello fellow Developers, sorry if it’s a dumb question, I’m still pretty new to scripting, I have a problem with which I’ve been struggling for the past hour, here are the details:

  1. What do you want to achieve?
    I’m trying to make a TextLabel which shows your current Health, when it’s full, it should show “Full Life” when you die, it should show “You Died”, and if it’s not full health, or dead, show the health you have.
  2. What is the issue?
    The issue is, when I spawn, it shows “Full Life”, as it should, but when I get damaged, it shows “Full Life” still, and when I die, it stays “Full Life”.
  3. What solutions have you tried so far?
    I’ve tried putting it into a while loop, but it showed “You Died” instead of “Full Life”.
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local health = character:WaitForChild("Humanoid").Health
local statistics = game.ReplicatedStorage.StatsGui:Clone()
local maxhealth = character:WaitForChild("Humanoid").MaxHealth
local healthdisplay = statistics.HealthTextLabel
local humanoid = character:WaitForChild("Humanoid")

statistics.Parent = player:WaitForChild("PlayerGui")

	if health == maxhealth then
	healthdisplay.Text = "Full Life"
	elseif health ~= maxhealth then
		healthdisplay.Text = health
	elseif humanoid.Died then
	healthdisplay.Text = "You Died"
	end

If you are wondering, this script is in StarterCharacterScripts and the StatsGui is the Gui with Health, Food and Water, and they are direct childrens of StatsGui, thanks in advance for any help you are gonna give me, hope you have a good day/evening, and see ya next time :smiley:.

With how your code is now, it will only ever check the health once. In order to repeatedly check the player’s health, you could put that if statement in a while loop or connect it to an event.

humanoid.Changed:Connect(function()
     -- the given function will run every time something changes in the Humanoid
     if health == maxhealth then
          healthdisplay.Text = "Full Life"
     elseif health ~= maxhealth then
		healthdisplay.Text = health
     elseif humanoid.Died then
          healthdisplay.Text = "You Died"
     end
end)
1 Like

Sorry for the late response, but your script does not work still :sweat:, this time, if I put your script and the while loop, it starts saying “Health”, and then changes to “Full Life”, but does not change anything, only with yours, doesn’t change anything and I already tried using a while loop and doesn’t work, but thanks for trying to help me :smiley:

This is because health & maxhealth are both saved as Number datatypes, and nowhere in the script does it specify them changing at all

@Jzwhale That doesn’t do much, as that changes for every single individual property resulting in unnecessary changes to frequently occur, what you want instead is the Humanoid.HealthChanged Event, that only fires when the property of Humanoid.Health is changed

This is more better & fits the situation at hand here, but what you don’t want to do is create variables that you aren’t going to define later on after changing the UI of the Text as it’ll never be updated unless it’s re-defined as something else (It’s better to just reference the property of Humanoid.Health/Humanoid.MaxHealth instead)

Same goes for Humanoid.Died as well, these Events were made specifically to handle actions such as these

local player = game:GetService("Players").LocalPlayer
local PlrGui = player:WaitForChild("PlayerGui")
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild("Humanoid")

local statistics = game.ReplicatedStorage.StatsGui:Clone()
local healthdisplay = statistics.HealthTextLabel

statistics.Parent = PlrGui

local DetectHealth
local function HealthChanged(NewHealth)
    if NewHealth == humanoid.MaxHealth then
        healthdisplay.Text = "Full Life"
    else
        healthdisplay.Text = NewHealth
    end
end

local function Died()
    DetectHealth:Disconnect()

    healthdisplay.Text = "You Died"
end

DetectHealth = humanoid.HealthChanged:Connect(HealthChanged)
humanoid.Died:Connect(Died)

Thanks, it worked fine, the only problem (which is not related to your script) is, it says 7.45264825184 or numbers like that, how would I round it to a full number? otherwise thanks for your help :smiley:

This is because Humanoid.Health is not actually rounded, and it just returns back as the original value from the difference of the Health

Thankfully though, we have a math function for this occasion called math.round, and it’s pretty straightforward on what it does lol

Just create a variable within your HealthChanged function, and make the Text display that Rounded Health instead of the NewHealth:

local RoundedHealth = math.round(NewHealth)
healthdisplay.Text = RoundedHealth

Where should I put it in the script?

Please make sure to re-read what I said before sending another post :slight_smile:

local function HealthChanged(NewHealth)
    local RoundedHealth = math.round(NewHealth)
    
    if NewHealth == humanoid.MaxHealth then
        healthdisplay.Text = "Full Life"
    else
        healthdisplay.Text = RoundedHealth
    end
end

Yeah sorry, I didn’t get if I had to do it at the start or after HealthChanged, not so good at scripting, sorry :sweat_smile:

Edit: It’s working fine now! You saved my day! (and probably the week :sweat_smile:)

1 Like

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