Hi, this health GUI doesn't work correctly

I’m making a health GUI split into 3 sections. It will not work correctly, however. When the character’s health is above 1 and below 100, it doesn’t display the correct decal layer. I haven’t tried anything from other sites yet, I’ve just been stuck here trying to figure out why it won’t work correctly.

This is the script I’m using:


print("Running")
wait(0.01)
P = script.Parent.Parent.Parent.Character.Humanoid
G = script.Parent

while true do
wait(.01)
if P.Health == 100 then
print("Health")
		script.Parent.HPiiii.Visible = false
		script.Parent.HPiii.Visible = true
		script.Parent.HPii.Visible = false
		script.Parent.HPi.Visible = false
		script.Parent.HP.Visible = false
end
	
	-- Full
	
if P.Health >= 66 and P.Health <= 99 then
print("Health")

		script.Parent.HPiiii.Visible = false
		G.HPiii.Visible = false
		script.Parent.HPii.Visible = true
		script.Parent.HPi.Visible = false
		script.Parent.HP.Visible = false
end	
	
	-- 2/3 ^
	
if P.Health > 0 and P.Health <= 65 then
print("Health")

		script.Parent.HPiiii.Visible = false
		script.Parent.HPiii.Visible = false
		script.Parent.HPii.Visible = false
		script.Parent.HPi.Visible = true
		script.Parent.HP.Visible = false

end	
	
 -- 1/3 ^	
	
if P.Health <= 0 then
print("Health")
		script.Parent.HPiiii.Visible = false
		script.Parent.HPiii.Visible = false
		script.Parent.HPii.Visible = false
		script.Parent.HPi.Visible = false
		script.Parent.HP.Visible = true

end	
	
	-- None
	
print("Still Working")
end

Each HP is for each section. (ex. “HPiiii” = 3/3 + 1 HP, “HPiii” = 3/3 HP, and so on.) The fourth decal is for a shielded HP, meaning you have an extra HP (kind of). I really don’t know why it won’t work; it might be because of the values I’ve put in. I’ll try to fix it in the meantime after I eat my lunch. Thanks for reating!

Try this.

local player = game.Players.LocalPlayer
repeat task.wait() until player.Character
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

local parentFrame = script.Parent

local hp = parentFrame:WaitForChild("HP")
local hpi = parentFrame:WaitForChild("HPi")
local hpii = parentFrame:WaitForChild("HPii")
local hpiii = parentFrame:WaitForChild("HPiii")


function healthChanged(health)
	if health >= 100 then
		hp.Visible = false
		hpi.Visible = false
		hpii.Visible = false
		hpiii.Visible = true
	elseif health >= 66 then
		hp.Visible = false
		hpi.Visible = false
		hpii.Visible = true
		hpiii.Visible = false
	elseif health > 0 then
		hp.Visible = false
		hpi.Visible = true
		hpii.Visible = false
		hpiii.Visible = false
	else
		hp.Visible = true
		hpi.Visible = false
		hpii.Visible = false
		hpiii.Visible = false
	end
end


humanoid.HealthChanged:Connect(healthChanged)

Note that I did not include a HPiiii because it does not seem to be used in your code as its visibility is never set to true. If this is by accident, this might also explain why you suddenly skipped from 66 to 0 in your health incrementation.

Let me know if you have any issues or further questions.

1 Like

You forgot to make the “hpiiii”'s visible setting set to false, I’ll do that and test it.

Yeah, I didn’t really know what HPiiii was doing there since you aren’t actually using it in your code. Let me know how it goes.

It didn’t work, notice how the HP is layered? All of the images are visible.

Can you send me a video of this in action? Also, if there are any errors in the output, let me know of them.

robloxapp-20211122-1304038.wmv (685.1 KB)

It appears you are indexing character with “nil”. I’ll check the script and see what’s wrong.

I didn’t see anything wrong. I’m not sure if there’s code misplaced, or something needs to be swapped.

Okay. It seems by the error that you are using a Script instead of a LocalScript. You should always use a LocalScript for player gui handling.

Yeah, I tried that first and it didn’t work. I’ll convert it to a LocalScript.

It works now. The damage bricks I’ll be using will damage the player by 1 section of their health. I’ll incorporate the shield mechanic after.

2 Likes

Awesome to hear. Best of luck on the project!

Thank you! It’s going to be a project similar to SUPER CHECK POINT. I have the lives system ready as well (SUPER CHECK POINT is just an obby with 30 stages and normally 5 lives.)

Oo sounds fun. Hope it goes well.