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

Humanoid.HealthChanged does not work?

Not sure why this isn't working. The text to display the health just stays to its default set (which is "null") I've also tried (function(health) but that doesn't work either. I've looked at articles, and they didn't help. Here's the script:

--Status
local player = game.Players.LocalPlayer
local HealthNav = script.Parent.Frame.StatusFrame.HealthFrame.Percentage
local HealthPercent = script.Parent.Frame.StatusFrame.HealthFrame.Percentage.Text
local Health = player.Character.Humanoid
	
player.Character.Humanoid.HealthChanged:Connect(function()
	HealthPercent = Health.Health .. "/" .. Health.MaxHealth .. "%"
	print(player.Name .. "'s Health is " .. Health.Health .. "/" .. Health.MaxHealth .. "%")
	if Health.Health == Health.MaxHealth then
		HealthNav.TextColor3 = Color3.new(255/255, 255/255, 255/255)
	elseif Health.Health >75 then
		HealthNav.TextColor3 = Color3.new(126/255, 255/255, 124/255)
	elseif Health.Health <75 and Health.Health >25 then
		HealthNav.TextColor3 = Color3.new(255/255, 202/255, 41/255)
	elseif Health.Health <25 then
		HealthNav.TextColor3 = Color3.new(255/255, 53/255, 53/255)
	end
end)

The colors work, the print works, but not the HealthPercent. Can someone please explain this? It would be appreciated.

4 comments

0

Usually I would use a remote event to deal damage but to me, I can't really see what is wrong. Are there any errors in console?

0

No errors in the Output. The test just displays "null" as a red text, since I set my health to 1.

0

what this script needed for?

0

To display the localplayers health inside a Gui.

1 answer

✓ Accepted answer

On line 04, you have this path set to a variable:

local HealthPercent = script.Parent.Frame.StatusFrame.HealthFrame.Percentage.Text

The line is saying:

Get whatever Text the "Percentage" GuiObject has and store it as a string value to this "HealthPercent" variable. This is now only a variable with some string value. Changing this variable to something else will NOT update the Percentage Text value!

To fix this, remove the .Text part in the path and use it inside of the event function.

To also clean up the code; You can use Color3.fromRGB() instead of the .new() variant. This clears up the clutter in the code since all you need to pass are the color values between 0-255.

--Recommend to use this method when you know you'll be using a service from Roblox.
--It helps knowing what the script will be working with.
local PlayersService = game:GetService("Players")

local Player = PlayersService.LocalPlayer
--Fun shorthand to always have the character available for the script.
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

--I'm assuming this is a LS under a gui. It's hard to read all these paths
--so I recommend separating them into more variables.
local HealthNav = script.Parent.Frame.StatusFrame.HealthFrame.Percentage
local HealthPercent = script.Parent.Frame.StatusFrame.HealthFrame.Percentage

Humanoid.HealthChanged:Connect(function(currentHealth)
	local roundedHealth = math.floor(currentHealth +.5)
    HealthPercent.Text = roundedHealth  .. "/" .. Humanoid.MaxHealth .. "%"

	if currentHealth >= Humanoid.MaxHealth then
        HealthNav.TextColor3 = Color3.fromRGB(255, 255, 255)
    elseif currentHealth >= 75 and currentHealth < Humanoid.MaxHealth then
        HealthNav.TextColor3 = Color3.fromRGB(126, 255, 124)
    elseif currentHealth < 75 and currentHealth >= 25 then
        HealthNav.TextColor3 = Color3.fromRGB(255, 202, 41)
    elseif currentHealth < 25 then
        HealthNav.TextColor3 = Color3.fromRGB(255, 53, 53)
    end
end)

I had also cleaned up the conditions in the if-statements. Make sure those are correct. I also recommend looking into using a table to lower the line count even more to increase readability.

Let me know if I had missed anything or you need anything explained.

1 comment

0

Thank you very much! Now I see why HealthPercent doesn't work. Also, thanks for the recommendations and clean ups!

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