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
8 years ago
01humanoid = game.Players.LocalPlayer.Character
02 
03humanoid.Humanoid.Changed:connect(function()
04 
05            if humanoid.Humanoid.Health == 100 then
06                script.Parent.Frame.Size = UDim2.new(0, 500, 0, 20)
07 
08            else if humanoid.Humanoid.Health <= 99  and humanoid.Humanoid.Health > 91 then
09                script.Parent.Frame.Size = UDim2.new(0, 450, 0, 20)
10 
11            else if humanoid.Humanoid.Health <= 90 and humanoid.Humanoid.Health > 81 then
12                script.Parent.Frame.Size = UDim2.new(0, 400, 0, 20)
13 
14            else if humanoid.Humanoid.Health <= 80 and humanoid.Humanoid.Health > 71 then
15                script.Parent.Frame.Size = UDim2.new(0, 350, 0, 20)
View all 51 lines...

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
8 years ago
Edited 8 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:

1humanoid = game.Players.LocalPlayer.Character

try instead:

1local player = game.Players.LocalPlayer
2local character = player.Character
3local 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.

01local player = game.Players.LocalPlayer
02local character = player.Character
03local human = character.Humanoid
04overlay = <PATH TO YOUR FRAME> (ie: script.Parent.HPFrame)
05 
06--First set your health to your max health
07human.Health = human.MaxHealth
08 
09--Functions
10function update()
11 --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.
12    overlay.Size = UDim2.new(0, (human.Health / human.MaxHealth) * 500, 0,20)
13 
14human.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:

01overlay.Size = UDim2.new(0, (human.Health / human.MaxHealth) * 500, 0,20)
02 
03    if human.Health < (human.MaxHealth / 2) and human.health > (human.MaxHealth / 4) then
04        over.HPOverlay.Visible = false
05 
06    end
07 
08    if human.Health < (human.MaxHealth / 4) and human.health > 0 then
09        over.HPOverlay.Visible = false
10        over.HPOverlayYellow.Visible = false
11    end
12 
13    if human.Health > (human.MaxHealth / 4) and human.health < (human.MaxHealth / 2) then
14        over.HPOverlay.Visible = false
15        over.HPOverlayYellow.Visible = true
View all 22 lines...

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 — 8y
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 — 8y
Ad

Answer this question