Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to fix this health text not setting the proper text?

--// Varibles \\--
local Plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local Character = Plr.Character or Plr.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HealthGui = script.Parent


Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	local HealthColor = Color3.fromRGB(255,0,0):Lerp(Color3.fromRGB(85,255,0),Humanoid.Health/Humanoid.MaxHealth)
	local HealthChange = Humanoid.Health/Humanoid.MaxHealth
	HealthGui.Health.Meter:TweenSize(UDim2.new(HealthChange,0,1,0),"In","Linear",1)
	HealthGui.Health.Meter.BackgroundColor3 = HealthColor
end)


local Text = HealthGui.Health.Text

Text = ("Health: "..Humanoid.Health)
Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	Text = ("Health: "..Humanoid.Health)
end)

1 comment

0

Try removing the parenthesis when you change the text

1 answer

✓ Accepted answer

This is a common misconception that happens!

You said on line 16

local Text = HealthGui.Health.Text

What this does is: it saves the variable "Text" with whatever text was inside the TextLabel.

So say the text inside was "Health: 100",

The variable text will now be set to "Health: 100"...

Therefore when you then change it and say Text = ("Health: " .. Humanoid.Health) you're simply changing the variable local Text, not the TextLabel's text.

To fix this, it's really simple:

(I kept everything the way you had it, simply added a little change)

--// Varibles \\--
local Plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local Character = Plr.Character or Plr.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HealthGui = script.Parent


Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    local HealthColor = Color3.fromRGB(255,0,0):Lerp(Color3.fromRGB(85,255,0),Humanoid.Health/Humanoid.MaxHealth)
    local HealthChange = Humanoid.Health/Humanoid.MaxHealth
    HealthGui.Health.Meter:TweenSize(UDim2.new(HealthChange,0,1,0),"In","Linear",1)
    HealthGui.Health.Meter.BackgroundColor3 = HealthColor
end)


local Text = HealthGui.Health -- define the textLabel

Text.Text = ("Health: "..Humanoid.Health) -- Change the textLabel's text
Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    Text.Text = ("Health: ".. Humanoid.Health) -- same here
end)

I also wrote a little neater version, if you want to use it...

--// Variables \\--
local Plr = game.Players.LocalPlayer --or game.Players.PlayerAdded:Wait() this is unnecessary, trust me. could also mess you up.
local Character = Plr.Character or Plr.CharacterAdded:Wait() -- capitalize the :Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HealthGui = script.Parent
local HealthLabel = HealthGui:WaitForChild("Health") -- define variable here

HealthLabel.Text = string.format("Health: %i", math.floor(Humanoid.Health + 0.5))
Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    local HealthPercentage = Humanoid.Health/Humanoid.MaxHealth
    local HealthColor = Color3.fromRGB(255,0,0):Lerp(Color3.fromRGB(85,255,0), HealthPercentage)
	-- last parameter in tweenSize set to true in case health is rapidly changed and we want to overlap an existing tween
    HealthLabel.Meter:TweenSize(UDim2.new(HealthPercentage, 0, 1, 0),"In","Linear",1,true)
    HealthLabel.Meter.BackgroundColor3 = HealthColor

    HealthLabel.Text = string.format("Health: %i", math.floor(Humanoid.Health + 0.5)) -- no need for two connections
end)

1 comment

0

please don't forget to accept my answer if it worked :)

This discussion is preserved as it was. New answers, comments, and votes are closed.