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

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

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

✓ Accepted answer

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)

2 comments

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.

0

Is the script in "Front"?

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

1 comment

0

Nope. Doesn't work. :/

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