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

Health Bar Gui, working in studio but not the game?

Asked by
Kymaraaa 116
7 years ago
humanoid = game.Players.LocalPlayer.Character

humanoid.Humanoid.Changed:connect(function()

            if humanoid.Humanoid.Health == 100 then 
                script.Parent.Frame.Size = UDim2.new(0, 500, 0, 20)

            else if humanoid.Humanoid.Health <= 99  and humanoid.Humanoid.Health > 91 then 
                script.Parent.Frame.Size = UDim2.new(0, 450, 0, 20)

            else if humanoid.Humanoid.Health <= 90 and humanoid.Humanoid.Health > 81 then
                script.Parent.Frame.Size = UDim2.new(0, 400, 0, 20)

            else if humanoid.Humanoid.Health <= 80 and humanoid.Humanoid.Health > 71 then
                script.Parent.Frame.Size = UDim2.new(0, 350, 0, 20)

            else if humanoid.Humanoid.Health <= 70 and humanoid.Humanoid.Health > 61 then
                script.Parent.Frame.Size = UDim2.new(0, 300, 0, 20)

            else if humanoid.Humanoid.Health <= 60 and humanoid.Humanoid.Health > 51 then
                script.Parent.Frame.Size = UDim2.new(0, 250, 0, 20)

            else if humanoid.Humanoid.Health <= 50 and humanoid.Humanoid.Health > 41 then
                script.Parent.Frame.Size = UDim2.new(0, 200, 0, 20)

            else if humanoid.Humanoid.Health <= 40 and humanoid.Humanoid.Health > 31 then
                script.Parent.Frame.Size = UDim2.new(0, 150, 0, 20)

            else if humanoid.Humanoid.Health <= 30 and humanoid.Humanoid.Health > 21 then

            else if humanoid.Humanoid.Health <= 20 and humanoid.Humanoid.Health > 11 then
                script.Parent.Frame.Size = UDim2.new(0, 50, 0, 20)

            else if humanoid.Humanoid.Health <= 10 and humanoid.Humanoid.Health >= 1 then
                script.Parent.Frame.Size = UDim2.new(0, 0, 0, 20)

            else if humanoid.Humanoid.Health == 0 then
                print("you ded")
                end
            end
        end
    end
end
end
end
end
end
end
end
end
end)


Making a healthbar, not the best solution, but it's just a first try test.

It works fine in studio, but not the actual game itself. It's inside a ScreenGui in StarterGui & isn't a local script.

1 answer

Log in to vote
0
Answered by
Morficc 36
7 years ago
Edited 7 years ago

I am not an expert Roblox Lua guy by any means. I just started learning a few days ago. However, I do have a Healthbar script that works pretty well. I am not too sure why your script would fail in the game, but my best guess would be in the order and time in which everything loads in. Instead of creating a variable for:

humanoid = game.Players.LocalPlayer.Character

try instead:

local player = game.Players.LocalPlayer
local character = player.Character
local human = character.Humanoid

and then use "human" throughout the rest of the script.

Or maybe just add a slight delay at the beginning of the script, with a Wait(2)

Sorry for the guesswork, just trying to offer some suggestions.

Here is an en example of a more efficient healthbar script however. Maybe you could use some of it to optimize your own.

local player = game.Players.LocalPlayer
local character = player.Character
local human = character.Humanoid
overlay = <PATH TO YOUR FRAME> (ie: script.Parent.HPFrame)

--First set your health to your max health
human.Health = human.MaxHealth

--Functions
function update()
 --This will resize your HPFrame to a value according to your current health. The only consideration is to ensure your frame is sized to the 0 and 100% health marks.
    overlay.Size = UDim2.new(0, (human.Health / human.MaxHealth) * 500, 0,20) 

human.Changed:connect(update)

If you wanted to get really fancy, you could recolor your HPFrame at certain levels of health (Or if you are using an image, you could make different color images visible at different intervals. Here is an example:

overlay.Size = UDim2.new(0, (human.Health / human.MaxHealth) * 500, 0,20)

    if human.Health < (human.MaxHealth / 2) and human.health > (human.MaxHealth / 4) then
        over.HPOverlay.Visible = false

    end

    if human.Health < (human.MaxHealth / 4) and human.health > 0 then
        over.HPOverlay.Visible = false
        over.HPOverlayYellow.Visible = false
    end 

    if human.Health > (human.MaxHealth / 4) and human.health < (human.MaxHealth / 2) then
        over.HPOverlay.Visible = false
        over.HPOverlayYellow.Visible = true
    end

    if human.Health > (human.MaxHealth / 2) and human.health > 0 then
        over.HPOverlay.Visible = true
        over.HPOverlayYellow.Visible = true
    end
end

In this above example, I have three HPbars overlapped. A green one with zindex of 3, a yellow one with zindex of 2, and a red one with zindex of 1.

As my health drops, to below 50% (MaxHealth / 2), the green bar disappears and the yellow bar now shows (still sized correctly since it is within the same frame being shrunk)

As my health drops below 25% (MaxHealth / 4), the green and the yellow bars are hidden, allowing the red bar to show.

Just some food for thought.

0
I recommend using Scale (the first & third parameters in UDIM2.new) property, as Offset works w/ pixels, while Scale supports separate platforms, such as computers, tablets, etc. TheeDeathCaster 2368 — 7y
0
Good tip! I read about that after I already positioned all of my GUI elements by pixel. I plan on converting it all to scale soon, but haven't gotten around to it. Thanks for the reminder! Morficc 36 — 7y
Ad

Answer this question