Remember to use local variables.
Using local variables not only keeps your code neat and organized but "local variables are obtained faster than normal variables. This is because they are integrated into the current environment they were created in." - Wiki
Utilize LocalPlayer
You can put this script in your GUI (In a LocalScript) to keep from having to use a ton of Parents. You can instead utilize LocalPlayer. Your hierarchy seems to convey that you have Frame and two TextLabel's. One TextLabel is named 'Label' and the other is named something else You should place the Local Script inside the TextLabel that's not named Label
http://prntscr.com/ew3i9j
Comparing your health
You need a static (non-changing) number to compare your Health to, else you're going to be comparing your health to itself / 2. Your health will always be higher in this case, causing Frame to always be green.
Setting a Color3 color with Color3.FromRGB()
In your case, since you used (255,0,0) - Simply red, no variation with green and blue, this is not relevant. However, if you wanted a variation of red, this would be.
If you try setting a Color3 Value without dividing each number by 255, it will not display the color you're trying to use. However, if you use Color3.FromRGB(255,70,56) it will do this for you automatically and convert to the correct RGB color to display.
Waiting for objects to load
In Studio you won't have many errors pertaining to the script loading before objects. In online work, this will happen. To combat the "Object is not a valid member of Parent" error, you should use WaitForChild() and in this case wait for your Character as well.
local me = game.Players.LocalPlayer
repeat wait() until me.Character
local character = me.Character
local humanoid = character:WaitForChild("Humanoid")
local frame = script.Parent.Parent
local LabelTheScriptIsIn = script.Parent
local LabelInTheFrame = frame:WaitForChild("Label")
local maxHealth = 100
function updateHealth()
if humanoid.Health < ( maxHealth / 2 ) then
frame.Style = "ChatRed"
LabelTheScriptIsIn.TextColor3 = Color3.new(255, 0, 0)
LabelInTheFrame.TextColor3 = Color3.new(255, 0, 0)
else
frame.Style = "ChatGreen"
LabelTheScriptIsIn.TextColor3 = Color3.new(0, 0, 0)
LabelInTheFrame.TextColor3 = Color3.new(0, 0, 0)
end
LabelTheScriptIsIn.Text = humanoid.Health
end
humanoid.Changed:connect(function(property)
if property == "Health" or property == "Health_XML" then
updateHealth()
end
end)
3 comments
The script has not changed
i put then on a different line of code
Placing code on a different line makes absolutely no difference to a script. You can place everything on one line and it will still do exactly the same thing.