I am making a health meter that uses a frame that uses the style presets ChatRed
and ChatGreen
depending on weather the health is under half or over half, anyway, upon setting the health to 30 out of 100, the GUI remains on the ChatGreen
preset... instead of changing to the ChatRed
preset.
Here is the code:
function updateHealth() if game.Workspace[script.Parent.Parent.Parent.Parent.Parent.Parent.Name].Humanoid.Health < game.Workspace[script.Parent.Parent.Parent.Parent.Parent.Parent.Name].Humanoid.Health / 2 then script.Parent.Parent.Style = "ChatRed" script.Parent.TextColor3 = Color3.new(255, 0, 0) script.Parent.Parent.Label.TextColor3 = Color3.new(255, 0, 0) else script.Parent.Parent.Style = "ChatGreen" script.Parent.TextColor3 = Color3.new(0, 0, 0) script.Parent.Parent.Label.TextColor3 = Color3.new(0, 0, 0) end script.Parent.Text = game.Workspace[script.Parent.Parent.Parent.Parent.Parent.Parent.Name].Humanoid.Health end game.Workspace:WaitForChild(script.Parent.Parent.Parent.Parent.Parent.Parent.Name) updateHealth() game.Workspace[script.Parent.Parent.Parent.Parent.Parent.Parent.Name].Humanoid.Changed:connect(function() updateHealth() end)
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
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)
function updateHealth() if game.Workspace[script.Parent.Parent.Parent.Parent.Parent.Parent.Name].Humanoid.Health < game.Workspace[script.Parent.Parent.Parent.Parent.Parent.Parent.Name].Humanoid.Health / 2 then script.Parent.Parent.Style = "ChatRed" script.Parent.TextColor3 = Color3.new(255, 0, 0) script.Parent.Parent.Label.TextColor3 = Color3.new(255, 0, 0) else script.Parent.Parent.Style = "ChatGreen" script.Parent.TextColor3 = Color3.new(0, 0, 0) script.Parent.Parent.Label.TextColor3 = Color3.new(0, 0, 0) end script.Parent.Text = game.Workspace[script.Parent.Parent.Parent.Parent.Parent.Parent.Name].Humanoid.Health end game.Workspace:WaitForChild(script.Parent.Parent.Parent.Parent.Parent.Parent.Name) updateHealth() game.Workspace[script.Parent.Parent.Parent.Parent.Parent.Parent.Name].Humanoid.Changed:connect(function() updateHealth() end)
Try this...