Answered by
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:
1 | humanoid = game.Players.LocalPlayer.Character |
try instead:
1 | local player = game.Players.LocalPlayer |
2 | local character = player.Character |
3 | 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.
01 | local player = game.Players.LocalPlayer |
02 | local character = player.Character |
03 | local human = character.Humanoid |
04 | overlay = <PATH TO YOUR FRAME> (ie: script.Parent.HPFrame) |
07 | human.Health = human.MaxHealth |
12 | overlay.Size = UDim 2. new( 0 , (human.Health / human.MaxHealth) * 500 , 0 , 20 ) |
14 | 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:
01 | overlay.Size = UDim 2. new( 0 , (human.Health / human.MaxHealth) * 500 , 0 , 20 ) |
03 | if human.Health < (human.MaxHealth / 2 ) and human.health > (human.MaxHealth / 4 ) then |
04 | over.HPOverlay.Visible = false |
08 | if human.Health < (human.MaxHealth / 4 ) and human.health > 0 then |
09 | over.HPOverlay.Visible = false |
10 | over.HPOverlayYellow.Visible = false |
13 | if human.Health > (human.MaxHealth / 4 ) and human.health < (human.MaxHealth / 2 ) then |
14 | over.HPOverlay.Visible = false |
15 | over.HPOverlayYellow.Visible = true |
18 | if human.Health > (human.MaxHealth / 2 ) and human.health > 0 then |
19 | over.HPOverlay.Visible = true |
20 | over.HPOverlayYellow.Visible = true |
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.