I’m trying to make a text label that displays health numbers, so like 100/100 and if I get damaged it of course varies. But I do not know how to, I know it’s simple but I tried and it isn’t working. I made the bar that changes during damage but the numbers I do not have.
The main idea is that you’re looking for strings of the Humanoid’s Health and MaxHealth and concatenating them together along with a slash sign to get this. You can do this in any number of ways so long as those numbers are becoming strings.
Personally I just use format because it’s straightforward. I don’t think it’s better than concatenation but I tend not to mind the technical details there unless the performance difference is really noticeable.
string.format("%d/%d", Humanoid.Health, Humanoid.MaxHealth)
You would use the Health and MaxHealth property of Humanoid, which is part of the Character object that can be referenced by writing p.Character (where p is your LocalPlayer)
I hope this helps, goodluck on your developing career!
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local text = pathto.TextLabel
local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function()
text.Text = string.format("%d/%d", humanoid.Health, humanoid.MaxHealth)
end)
Thanks but it doesn’t seem to be working, is this a local script I place inside the textlabel?
It is a LocalScript
and inside a TextLabel
.
Don’t forget to set the actual parameters for this.
doesn’t seem to be working,. hmm
Pathto refers to the path from the script to the TextLabel, if the LocalScript is inside the TextLabel it would simply be script.Parent
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local text = script.Parent
local humanoid = character:WaitForChild("Humanoid")
humanoid.HealthChanged:Connect(function()
text.Text = string.format("%d/%d", humanoid.Health, humanoid.MaxHealth)
end)
Try this, I believe it might work now because HealthChanged
function is not working for me either.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local text = pathto.TextLabel
local humanoid = character:WaitForChild("Humanoid")
while true do
wait()
text.Text = string.format("%d/%d", humanoid.Health, humanoid.MaxHealth)
end
I have one question tho, I tried to keep my text as rich text but as soon as the health changes it goes back to normal how do I fix this
You should give the solution to taehyung since they wrote the actual code