|
| 19 Jan 2014 09:52 PM |
a basic health bar can be scripted as such:
HP.Changed:connect(function() B3.Size = UDim2.new((HP.Value/HP.MaxValue)*1, 0, 1, 0) end)
Easy peasy rice n cheesy
But what about multiple health bars? what if one bar dealt with hp 0 - 100, a second bar with 101 - 200, and a 3rd bar with 201 - 300? How would I do that? |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:01 PM |
local Bars = { BAR1, BAR2, BAR3, BAR4, etc, etc }
HP.Changed:connect(function() local health = HP.Value local bar_ind = math.floor(health/100) local rel_hp = health - bar_ind * 100 Bars[bar_ind] = UDim2.new( rel_hp/100, 0, 1, 0) end) |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:03 PM |
Actually, change math.floor to math.ceil. Also, change the Bars table to this:
local Bars = { [0] = BAR1, BAR1, BAR2, BAR3, BAR4, etc, etc } |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:21 PM |
Shortened the bar names to B1 and so on
HP = script.Parent.Health F = script.Parent.Frame local Bars = {[0] = F.B1, F.B2, F.B3}
HP.Changed:connect(function() local health = HP.Value local bar_ind = math.ceil(health/100) local rel_hp = health - bar_ind * 100 Bars[bar_ind] = UDim2.new( rel_hp/100, 0, 1, 0) end)
No output, yet doesn't do anything. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:28 PM |
I forgot .Size. >_>
HP = script.Parent.Health F = script.Parent.Frame local Bars = {[0] = F.B1, F.B1, F.B2, F.B3}
HP.Changed:connect(function() local health = HP.Value local bar_ind = math.ceil(health/100) local rel_hp = health - bar_ind * 100 Bars[bar_ind].Size = UDim2.new( rel_hp/100, 0, 1, 0) end)
Try that. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:31 PM |
HP.MaxValue is 300, when the Value reached 201, I got this error
22:29:51.586 - Players.xiaoxiao181.PlayerGui.HealthGUI.Script:9: attempt to index field '?' (a nil value) 22:29:51.588 - Script 'Players.xiaoxiao181.PlayerGui.HealthGUI.Script', Line 9 22:29:51.590 - stack end 22:29:51.591 - Disconnected event because of exception
Also, any value 200 or less resulted in the bars being sized negatively. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:35 PM |
Did you change the Bars table when you copy/pasted? The [0] = B1 isn't an accident.
Could you add a print(bar_ind) in there? |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:36 PM |
local rel_hp = health - (bar_ind - 1) * 100 |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:41 PM |
HP = script.Parent.Health F = script.Parent.Frame local Bars = {[0] = F.B1, F.B2, F.B3}
HP.Changed:connect(function() local health = HP.Value local bar_ind = math.ceil(health/100) print(bar_ind) local rel_hp = health - bar_ind * 100 Bars[bar_ind].Size = UDim2.new( rel_hp/100, 0, 1, 0) end)
22:36:36.643 - Players.xiaoxiao181.PlayerGui.HealthGUI.Script:10: attempt to index field '?' (a nil value) 22:36:36.644 - Script 'Players.xiaoxiao181.PlayerGui.HealthGUI.Script', Line 10 22:36:36.646 - stack end 22:36:36.647 - Disconnected event because of exception
The bars still move left to right when hp is increasing like it would if the bars were positively sized, but at 0, B1 is (-1, 0) at 1 its (-0.99, 0) and so on.
the printing resulted in labeling 1, 2, and 3. 0 - 100 is 1 101 - 200 is 2 201 - 300 is 3 When it gets to 3, I get the error message I showed you above. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:43 PM |
local Bars = {[0] = F.B1, F.B1, F.B2, F.B3}
The extra F.B1 was NOT a mistake. It must look like this. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:43 PM |
Also, don't forget my other post:
local rel_hp = health - (bar_ind - 1) * 100 |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:44 PM |
local rel_hp = health - (bar_ind - 1) * 100
Fixed the negative sizing issue, but I still get that error when it tries to size it from 201 - 300, 300 is the max value. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:47 PM |
local Bars = {[0] = F.B1, F.B2, F.B3}
What does ([0] =) have to do with anything? I don't understand that part. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:53 PM |
| He's making his table become 0-indexed, against roblox's (more like lua's) 1-indexed tables... So that he can do t[0] instead of t[1]. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 10:58 PM |
I don't fully understand that, but I'll go with it until I do.
Any way to fix the error?
Also, I feel inclined to say that if HP.Value == 50, and is then changed to 150, the first bar doesnt get fully sized. It stays the same as the second bar is being sized. So if its 50, the first bar goes 0.5, but if its changed to say, 125, the first bar stays 0.5 while the second bar goes 0.25 |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 11:03 PM |
| Ok, new update. I was looking at the bars wrong. 1- 100 seems to be sizing the second bar, and 101 - 200 is sizing the third bar. the first bar isnt being affected at all, which seems to explain why it gives me that error when it reaches 201 - 300 |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 11:08 PM |
Bars[bar_ind-1].Size = UDim2.new( rel_hp/100, 0, 1, 0)
i added a -1 after the bar_ind and that fixed the error. I'm having problems with the Zindex of the guis or maybe I labeled or colored them wrong.
B1 is yellow, B2 is orange, and B3 is red. 0 - 100 affects the yellow, 101 - 200 affects orange. I think if I label the red one as B1 and the yellow as B3 that should fix the visual problem. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 11:14 PM |
Ok I fixed the visual glitch, and the script is working without error but I am still having 2 more issues that need to be fixed. When the first bar is full at 100 HP, and then 1 or more HP is added, the second bar starts to fill. For example the HP is at 101, so the scale of the first bar is 1 and the second bar is 0.1, but if I change the HP back to 100, the second bar stays at 0.1 instead of changing to 0.
the second issue is that if HP is at 50, the first bar is at 0.5, but if I change the HP instantly to say... 125, the first bar stays at 0.5 while the second bar sizes to 0.25.
I could work around the second issue by using a for loop when increasing health, but the first issue isn't solved in this way. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 11:25 PM |
Yes. I'll fix that in a bit.
You need to use a for loop and set the values to 0 or 100. If i < bar_ind, set health to 1. Otherwise, if i > bar_ind, set health to 0. If i == bar_ind, set health to rel_hp / 100. |
|
|
| Report Abuse |
|
|
|
| 19 Jan 2014 11:42 PM |
I'm not sure I follow you. What is i? You mean the i in the for loop? I thought i was used to determine the values being affected?
for i = 1, 300, 1 do wait() HP.Value = i end
I don't think I would be able to use any if statements for that o.o Unless you can and I just don't know about it yet.
It's not the health im worried about anyway its the gui bar sizes. |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2014 12:04 AM |
Like I said, the gui bars are only affected when the value of HP is in their range.
the idea is merely for adjusting the gui bars so that i can use multiple bars to represent health beyond 100. using a for loop to add health works around the second glitch, but it does not fix the first one. |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2014 12:14 AM |
for i = #Bars, 0, -1 do if i < bar_ind then -- stuff elseif i > bar_ind then -- stuff else -- stuff end end
Replace the assignment line with those lines and fill in. |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2014 12:20 AM |
Why not:
bars = {} --the bars? cur = #bars -- last bar resized for i, v in pairs(bars) do v.ZIndex = i; --to make them visible in order? end
Humanoid.Health.Changed:connect(function() --replace Humanoid b = math.floor(Humanoid.Health/100) if b != cur then if cur > 1 then --I assume you have them all in a container of the size every bar is supposed to have bars[cur-1].Size = UDim2.new(0, 0, 1, 0) end bars[cur].Size = UDim2.new(0, 0, 1, 0) end if b > 1 then bars[b-1].Size = UDim2.new(1, 0, 1, 0) end bars[b].Size = UDim2.new(Humanoid.Health/100, 0, 1, 0) end) |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2014 02:33 PM |
I am so confused now... Idk what an assignment line is and the attempt Axel made was extremely full of holes. After trying to fill in certain areas with correct information all I managed to do was get the bars to extend beyond their container, not to mention I kept getting index field errors when the health was below 100. |
|
|
| Report Abuse |
|
|
|
| 20 Jan 2014 02:41 PM |
HP = script.Parent.Health F = script.Parent.Frame Bars = {[0] = F.B1, F.B2, F.B3}
HP.Changed:connect(function() local health = HP.Value local bar_ind = math.ceil(health/100) print(bar_ind) local rel_hp = health - (bar_ind - 1) * 100 Bars[bar_ind - 1].Size = UDim2.new(rel_hp/100, 0, 1, 0) end)
This is the result of all the work Fox and I put into getting this correct. I can use a for loop to increase health and it gets around one glitch. I can deal with that no worries. I actually like the idea of using a for loop to increase health. It's like a potion.
The only problem left is the gui size. Let's say HP.Value == 101. Since the script uses the scale of the gui to resize it, it might as well be a percentage, so it uses a decimal. At 101, the red bar is full, and the orange bar is at 0.01. When HP.Value changes to 100, the orange bar does not change the size to 0. The same is said against the orange bar vs the yellow bar when HP.Value is 201 to 200.
Now, I could just hide that by placing the HUD slightly over that end but I'd prefer to fix it. |
|
|
| Report Abuse |
|
|