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

Health Gui Not working?!

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
char = game.Players.LocalPlayer.Character
hum = char:WaitForChild("Humanoid")

wait(3)

hum.Changed:connect(function(update)
    script.Parent.Inner.Size = UDim2.new(hum.Health/289, 0, 0, 25)
        if hum.Health <= 20 then
            script.Parent.Inner.BackgroundColor3 = Color3.new(255, 0, 0)
        elseif hum.Health <= 60 then
            script.Parent.Inner.BackgroundColor3 = Color3.new(255, 221, 0)
        elseif hum.Health > 60 then
            script.Parent.Inner.BackgroundColor3 = Color3.new(12, 255, 0)
        end
    end)

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

There's a few problems here.

First, the Character is likely to not be loaded by the time this LocalScript runs. We have to wait for it.

Here's a cute way to do that:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()

Next, Color3 takes an r, g, and b that are in the range 0 to 1, not in the range 0 to 255.

You have to divide those parameters by 255.


You shouldn't have elseif when you mean else -- you want the last branch to always happen (and it will anyway) -- don't write something that means nothing.

You're also repeating yourself five times with script.Parent.Inner. Why not make a variable?

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local hum = char:WaitForChild("Humanoid")

wait(3)

local inner = script.Parent.Inner

hum.Changed:connect(function(update)
    inner.Size = UDim2.new(hum.Health/289, 0, 0, 25)
    if hum.Health <= 20 then
        inner.BackgroundColor3 = Color3.new(15, 0, 0)
    elseif hum.Health <= 60 then
        inner.BackgroundColor3 = Color3.new(1, 221/255, 0)
    else
        inner.BackgroundColor3 = Color3.new(12/255, 1, 0)
    end
end)

Where did 289 come from? If it's the humanoid's max-health (which would make sense) then you should probably just use that so that you can change the MaxHealth without having to update this script.

0
Thanks! This helped a lot! the 289 allowed the health bar not to cross over the GUI border i made. Is there an easier way? NonSpace 0 — 9y
Ad

Answer this question