Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

'if' statements not working?

Asked by 8 years ago

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

Log in to vote
2
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

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.

0
Thanks, that worked perfectly! SchonATL 15 — 8y
0
No problem. Pyrondon 2089 — 8y
Ad

Answer this question