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?
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)
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 …
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"
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!’
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.
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…
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)
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…