Code issues with the health bar

Hi! i would like to know how to do in my code “if health bar is == 50” and if life is <= 40 ‘if statement’ and show a text with the healthbar value, how i do that? :sad:
could someone help me with that please

 local players = game:GetService("Players")

    players.PlayerAdded:Connect(function(plr)
    	plr.CharacterAdded:Connect(function(char)
    		local gui = script.CustomHealthGui:Clone()
    		gui.Parent = char.Head
    		
    		local mainFrame = gui.MainFrame
    		mainFrame.Username.Text = plr.name
    		
    		local image = players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size100x100)
    		mainFrame.UserImageLabel.Image = image
    		
    		spawn(function()
    			while wait() do
    				mainFrame.HealthLabel.Text = tostring(math.floor(char.Humanoid.Health)).."/"..tostring(math.floor(char.Humanoid.MaxHealth))
    				mainFrame.HealthBar.Size = UDim2.fromScale((char.Humanoid.Health / char.Humanoid.MaxHealth), 0.4)	
    			end
    		end)
    		
    		if char.Humanoid.Health <= 50 then
    			print("SHOW TEXTLABEL")

    		end
    		
    	end)
    end)
1 Like

Could you explain this to me. If you are looking for a thing in your game to see if the health bar = 50 and the life bar <= 40(assuming the life bar is the character’s health), aren’t they the same thing? Isn’t the health bar equal to the health so they will never be different?

It is that I need to show a ‘textlabel’ when the life bar is at 50 saying something, like for example “a little more, keep it up” this is the idea and what I’m really looking for, I’m not sure how to do it … :frowning:

Ok, one thing I need to know, is this a local script or a normal script?

it’s a normal (server script) could you help me with that, please? :frowning:

usr

Absolutely, put in the top

local PLAYER = game.Players.LocalPlayer

then put in the if statement you already have,

if char.Humanoid.Health <= 50 then
   	PLAYER.PlayerGui.CustomHealthGui.MainFrame.WhateverLabelIsSupposedToBeHere.Visible = true
end

this should work, it might have an issue though because it isn’t a local script but it should be fine…

First, the if statement needs to be within the scope of the while loop:

while wait() do
    mainFrame.HealthLabel.Text = tostring(math.floor(char.Humanoid.Health)).."/"..tostring(math.floor(char.Humanoid.MaxHealth))
    mainFrame.HealthBar.Size = UDim2.fromScale((char.Humanoid.Health / char.Humanoid.MaxHealth), 0.4)
	if char.Humanoid.Health == 50 then
		print("SHOW TEXTLABEL")
	elseif char.Humanoid.Health <= 40 then
		print("Check2")
	end	
end

Now you need to create an additional TextLabel for this new text to appear, modify it to appear how you want but the major thing is that it needs to be a child of MainFrame. After that modify both of the print statements in code above to say, make sure to replace PUT THE NAME OF THE TEXT LABEL HERE with the name of your new Text Label (it cannot have spaces):
mainFrame.PUT THE NAME OF THE TEXT LABEL HERE.Text = "Sample Text"

2 Likes

OP should probably avoid using a while loop in general. I’m thinking the :GetPropertyChangedSignal or the .Changed event should be used, but this might just be my opinion though.

Also just an FYI, you can use spaces in your hierarchy by using :FindFirstChild or using square brackets (e.g mainFrame[‘Name With Spaces’].Text = ‘Hello!’

3 Likes

Yeah that is a much more efficient approach, and will save on a lot of resources OP

I never recommend using spaces in your file naming structure as it’s bad practice. Using camelCase is industry standard, so I recommend getting in the habit of using it.

2 Likes

Sorry guys… How could I show this on top of the billboard?

Set the anchorpoint to 0.5,0 and then it is position to 0.5,0,0,0. Ensure ClipsDescendants is false on the BillboardGui. Also ensure the textlabel is the first child of the Gui.

From what I see in picture, just switch the parents from wherever the thing you circle is in the screen gui or whatever, (or is that thing you circled not part of your game?) to the billboard gui…

The text did not change, what can I do? :frowning:

local players = game:GetService("Players")
local PLAYER = game.Players.LocalPlayer

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local gui = script.CustomHealthGui:Clone()
		gui.Parent = char.Head

		local mainFrame = gui.MainFrame
		mainFrame.Username.Text = plr.name

		local image = players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size100x100)
		mainFrame.UserImageLabel.Image = image

		spawn(function()
			while wait() do
				mainFrame.HealthLabel.Text = tostring(math.floor(char.Humanoid.Health)).."/"..tostring(math.floor(char.Humanoid.MaxHealth))
				mainFrame.HealthBar.Size = UDim2.fromScale((char.Humanoid.Health / char.Humanoid.MaxHealth), 0.4)	
				while wait() do
					mainFrame.HealthLabel.Text = tostring(math.floor(char.Humanoid.Health)).."/"..tostring(math.floor(char.Humanoid.MaxHealth))
					mainFrame.HealthBar.Size = UDim2.fromScale((char.Humanoid.Health / char.Humanoid.MaxHealth), 0.4)
					if char.Humanoid.Health <= 90 and char.Humanoid.Health >= 80 then
						print("SHOW TEXTLABEL")
						script.CustomHealthGui.MainFrame.Percent.Text = "WOW HIT HARD!"
					elseif char.Humanoid.Health <= 40 then
						print("Check2")
						script.CustomHealthGui.MainFrame.Percent.Text = "HOLD ON!"
					end	
				end
				end
		end)	
	end)
end)

11111111

1 Like

Is the humanoid’s health ever going under 40? Are any of the prints being printed?

Try adding this outside of the spawn:

char.Humanoid.Changed:Connect(function(property)
    if property == 'Health' then
        print(char.Humanoid.Health)
    else
        print(property)
    end
end)

Another thing,

Should be plr.Name not plr.name.

This is because it is in a normal script, try one of these two things, put the whole script as a local script or, put a local script in the “percent” text label and then put put a remote event in that.
And instead of putting that in the normal script…

Put

script.CustomHealthGui.MainFrame.Percent.LocalScript.RemoteEvent:FireClient(PLAYER)

Then in the local script

Function onEvent()
Do all the changing text in here
end
script.RemoteEvent.OnClientEvent:Connect(onEvent)

I recommend the second version, while it takes longer it keeps the original stuff in a normal script

1 Like

It shouldn’t make a difference whether it’s a server or localscript. He’s modifying a billboardgui

Aren’t billboard GUIs still part of starter GUI?

Nah, they’re basically never inside of the startergui.

1 Like