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

'if' statements not working?

I've placed prints throughout the script and the print showed up in the function but not inside any of the if statements. Any help you can provide is appreciated.

local player = script.Parent.Parent.Parent.Parent.Parent
local character = player.Character
local health = character.Humanoid.Health
function Hearts()
	if health == 0 then
		script.Parent.Heart1.ImageTransparency = 0.5
		else
		script.Parent.Heart1.ImageTransparency = 0
	end
	--
	if health >= 25 and health <= 49 then
		script.Parent.Heart2.ImageTransparency = 0.5
	else
		script.Parent.Heart2.ImageTransparency = 0
	end
	--
	if health >= 50 and health <= 74 then
		script.Parent.Heart2.ImageTransparency = 0.5
	else
		script.Parent.Heart2.ImageTransparency = 0
	end
	--
	if health >= 75 and health <= 99 then
		script.Parent.Heart2.ImageTransparency = 0.5
	else
		script.Parent.Heart2.ImageTransparency = 0
	end
	--
end

character.Humanoid.Changed:connect(Hearts)

1 answer

✓ Accepted answer

When you define the health variable, it records the value of character.Humanoid.Health at that instant, and doesn't change. To fix this, all you need to do is (uncomment the if statements, of course, and) put the 'health' variable within the function.

local player = script.Parent.Parent.Parent.Parent.Parent
local character = player.Character
function Hearts()
	local health = character.Humanoid.Health
    if health == 0 then
        script.Parent.Heart1.ImageTransparency = 0.5
        else
        script.Parent.Heart1.ImageTransparency = 0
    end
    if health >= 25 and health <= 49 then
        script.Parent.Heart2.ImageTransparency = 0.5
    else
        script.Parent.Heart2.ImageTransparency = 0
    end
    if health >= 50 and health <= 74 then
        script.Parent.Heart2.ImageTransparency = 0.5
    else
        script.Parent.Heart2.ImageTransparency = 0
    end
    if health >= 75 and health <= 99 then
        script.Parent.Heart2.ImageTransparency = 0.5
    else
        script.Parent.Heart2.ImageTransparency = 0
    end
end

character.Humanoid.Changed:connect(Hearts)

Hope this helped.

2 comments

0

Thanks, that worked perfectly!

0

No problem.

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