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

Why wont GUI frame style is not changing when health is low?

Asked by 7 years ago

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)

2 answers

Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

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)


Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
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...

0
The script has not changed AlphaGamer150 101 — 7y
0
i put then on a different line of code pegasuswa 6 — 7y
0
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. jotslo 273 — 7y

Answer this question