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

How to make this change colors depending on the player's health?

Asked by 10 years ago

I'm wanting to know if it's possible to make this change colors as you have less and less health. Full health = Neon Green 75 Health = Yellow green 65 Health = Yellow 50 Health = Orange 24 And below = Red

local PositionX = script.Parent.Position.X.Offset
local initX = script.Parent.Size.X.Offset

while true do 
wait()
local PlayerHealth = script.Parent.Parent.Parent.Parent.Parent.Parent.Character.Humanoid.Health 
local PlayerMaxHealth = script.Parent.Parent.Parent.Parent.Parent.Parent.Character.Humanoid.MaxHealth 
local Pie = (PlayerHealth / PlayerMaxHealth)  
script.Parent.Size = UDim2.new( 0, initX*Pie, 0, 20) 
 end

2 answers

Log in to vote
0
Answered by 10 years ago

Here you go!

Player = script.Parent.Parent.Parent.Parent.Parent.Parent
Character = Player.Character
Human = Character.Humanoid
GUI = script.Parent --Remember to change this! If the script is inside of the thing you wanna color then leave it alone.

function Check() --Using a function, I can connect this to the Changed event so I can change the color whenever the health is changed as opposed to a while loop
    if Human.Health == 100 then
        GUI.BackgroundColor3 = Color3.new(0, 1, 0)
    elseif Human.Health <= 99 and Human.Health > 65 then
        GUI.BackgroundColor3 = Color3.new(0.8, 1, 0)
    elseif Human.Health <= 65 and Human.Health > 50 then --Using this creates a range so it's not 1     color always
        GUI.BackgroundColor3 = Color3.new(1, 1, 0)
    elseif Human.Health <= 50 and Human.Health > 24 then
        GUI.BackgroundColor3 = Color3.new(1, 0.4, 0)
    elseif Human.Health <24 then
        GUI.BackgroundColor3 = Color3.new(1, 0, 0)
    end
end

Human.Changed:connect(function (property)
    if property == Health then
        Check()
    end
end)
0
Hmm...I'm still having problems. The GUI's children looks like this: HealthBar>NewFrame>HealthBar>Front. Front is where the GUI is stored at. TheRings0fSaturn 28 — 10y
0
Is the script in "Front"? XanthicDragon 38 — 10y
Ad
Log in to vote
0
Answered by
hudzell 238 Moderation Voter
10 years ago
if PlayerHealth > 75 then
    script.Parent.Color = Color3.new(0,1,0)
elseif PlayerHealth < 75 then
    script.Parent.Color = Color3.new(.8,1,0)
elseif PlayerHealth < 65 
    script.Parent.Color = Color3.new(1,1,0)
elseif PlayerHealth < 50
    script.Parent.Color = Color3.new(1,.5,0)
elseif PlayerHealth < 25
    script.Parent.Color = Color3.new(1,0,0)
end
0
Nope. Doesn't work. :/ TheRings0fSaturn 28 — 10y

Answer this question