Image won't change depending on health

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

Try

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

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

Doesn’t seem to work, this is the error i got: 19:15:10.696 Players.Ballora544.PlayerGui.HealthGUI.Frame.Frame.ImageLabel.LocalScript:6: attempt to index number with ‘Changed’ - Client - LocalScript:6

Turns out, this was the solution! We just had to place a > at the and newvalue, I’m gonna mark this solution since it’s basically only missing one symbol. Thank you so much, you helped me a lot on this.

Full script:

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

humanoid.HealthChanged:Connect(function(newvalue)
	if newvalue <= 100 and newvalue >= 75 then
		image.Image = "http://www.roblox.com/asset/?id=7917732393"
	end

	if newvalue <= 75 and newvalue >= 50  then
		image.Image = "http://www.roblox.com/asset/?id=7917733637"
	end

	if newvalue <= 50 and newvalue >= 35 then
		image.Image = "http://www.roblox.com/asset/?id=7917734910"
	end

	if newvalue <= 35 and newvalue >= 20 then
		image.Image = "http://www.roblox.com/asset/?id=7917736411"
	end

	if newvalue <= 20 then
		image.Image = "http://www.roblox.com/asset/?id=7917737512"
	end
end)

1 Like
local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer or plrs.PlayerAdded:Wait()
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoid = character:WaitFirstChild("Humanoid")
local image = script.Parent
local healths = {100, 75, 50, 35, 20}
local assetIds = {7917732393, 7917733637, 7917734910, 7917736411, 7917737512}

humanoid.HealthChanged:Connect(function(newvalue)
	for i = 1, #healths do
		if newvalue <= healths[i] and newvalue > (healths[i+i] or 0) then
			image.Image = "http://www.roblox.com/asset/?id="..assetIds[i]
		end
	end
end)