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

Help with showing changing health, is it the proper way?

Asked by
FiredDusk 1466 Moderation Voter
7 years ago

Is this the proper way of writing this script in a local script? The code is changing the text and color of the text based on the health percentage.

local plr = game.Players.LocalPlayer
while true do
    script.Parent.Text = math.floor(plr.Character.Humanoid.Health) .. "%"
    wait(.1)
    if plr.Character.Humanoid.Health <=100 then
        script.Parent.TextColor3 = Color3.new(0/255,255/255,0/255)

        if plr.Character.Humanoid.Health <=75 then
            script.Parent.TextColor3 = Color3.new(235/255,169/255,1/255)


            if plr.Character.Humanoid.Health <=50 then
                script.Parent.TextColor3 = Color3.new(227/255,122/255,2/255)


                if plr.Character.Humanoid.Health <=30 then
                    script.Parent.TextColor3 = Color3.new(232/255,89/255,22/255)


                    if plr.Character.Humanoid.Health <=10 then
                        script.Parent.TextColor3 = Color3.new(255/255,0/255,0/255) 
                    end
                end
            end
        end
    end
end


1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

Use the HealthChanged event, much easier. I edited the colors a little. Change 'em if you want.

local player=game.Players.LocalPlayer
local humanoid=player.Character.Humanoid


humanoid.HealthChanged:connect(function(health)
    if  health==humanoid.MaxHealth then
        script.Parent.TextColor3=Color3.new(0,1,0) --green 
    elseif  health>=75 then
        script.Parent.TextColor3=Color3.new(1,1,0)
    elseif  health>=50 then
        script.Parent.TextColor3=Color3.new(195/255, 200/255, 0)
    elseif  health>=30 then
        script.Parent.TextColor3=Color3.new(255/255, 65/255, 0)
    elseif health<=10 then
        script.Parent.TextColor3=Color3.new(1,0,0) --red
    end
end)
Ad

Answer this question