Image won't change depending on health

I’ve been trying to make a heart icon that changes images depending on the player’s health. Basically, being more broken and broken.

Here’s my script:

local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = character:FindFirstChild("Humanoid")
local image = script.Parent

humanoid.HealthChanged:Connect(function(newvalue)
	if newvalue <= 100 then
		image.Image = "https://www.roblox.com/library/7917732450/100hpicon"
	end
	
	if newvalue <= 75 then
		image.Image = "https://www.roblox.com/library/7917733668/75hp"
	end
	
	if newvalue <= 50 then
		image.Image = "https://www.roblox.com/library/7917734933/50hp"
	end
	
	if newvalue <= 35 then
		image.Image = "https://www.roblox.com/library/7917736453/35hpicon"
	end
	
	if newvalue <= 20 then
		image.Image = "https://www.roblox.com/library/7917737527/20hpicon"
	end
end)

No matter what I do, the image won’t change when the health is 75, or 50, etc. I’m really clueless as to what I did wrong, so any help would be appreciated. (Yes, the local script is inside the image).

FOR SOLUTION: Make sure to add a > at and newvalue = (>=)

1 Like

To change the image, the normal URL won’t work. Instead, you need to replace all

image.Image = "https://www.roblox.com/library/id/name" (Normal URLS)

to

image.Image = "rbxassetid://7917737527" (rbxassetid://)

1 Like

I Will try it, I usually forget to use that URL lol

Still doesn’t work, thank you for letting me know about the URL error though!

perhaps try to use elseif instead of creating several different if statements then. also, try printing the newvalue variable just to make sure.

example would be

if v1 == v2 then
--do stuff
elseif v3 == v4 then
--do stuff
end

Edit: Forgot to ask, did you get any error? Because there is a ) missing on the last end.

Did your player take damage and it won’t update or are you changing the health through the properties window?

You can use a white image and change it’s ImageColor3 property instead.

Ah no, I just forgot to select that when copying the script to paste it here, my bad.

Player is taking damage and won’t update.

Dunno if you can do that with different images, they’re all IDs for different images

Example:
75hp
100hpicon

Oh, I though you are making a health bar that changes it’s color based on health.

No worries, already done that!
Screen Shot 2021-11-06 at 16.26.47
(The health bar will change color depending on health, it’s the blue-ish outline around the circle)

The elseif in this case would jsut make the first true and then it won’t continue since it is 100 or less

oh yeah, didn’t notice that. in that case you would have to add an and statement to make sure the HP are in the specific area.

Example:

if newvalue <= 100 and newvalue > 75 then
-- do stuff
end
1 Like

Still doesn’t seem to work… Maybe it’s the humanoid.HealthChanged? I probably used it in a wrong way and I’m not realizing it.

I don’t see anything wrong with it right now. Try printing newvalue when the function starts and check if and what it prints, perhaps that helps.

I get this, any idea on what it means?

The humanoid is nil. Perhaps it’s not loaded in when it’s needed, so replace :FindFirstChild() with :WaitForChild() in Line 3.

It does something, however the image just disappears, doesn’t appear again even if I healed to max HP

Edit: It re-appears when you respawn

I don’t know if this helps at all but Humanoid.HealthChanged doesn't seem to work after the player dies and respawns - #3 by BostonWhaIer