You can write your topic however you want, but you need to answer these questions:
I have made a overhead health tag, but I cannot seem to define when a player loses health. I tried:
Is this connected to an event by chance? Also, what is your main goal here? Are you simply looking to change the size of the bar to a correlating UDim2 value everytime as well as some text?
You’ll want to use a variable to save the last health.
local lasthealth = humanoid.Health
humanoid:GetPropertyChangedSignal("Health"):Connect(function()
if humanoid.Health < lasthealth then
--less
end
lasthealth = humanoid.Health
end)
Alright, well that might be an issue with the way you have stuff defined in your script. Also, it might be easier to simply do the following for every single HealthChange, that way you don’t have to code everything in custom.
Here is a quick example. There are more efficient ways of doing this but you can feel free of basing stuff around this and going from there if you’d like. I’m assuming this is a LocalScript, although correct me if I’m wrong here.
Also is there a reason why you’re making the bars visibility false once they die? If you wanted you could add that back but judging by the fact that the UI would probably reset on spawn if this is starterCharacter or in UI which is defined with ResetOnSpawn, it might not even be needed.
local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local healthtext = -- wherever healthtext is
local bar = -- wherever bar is
char:WaitForChild("Humanoid").HealthChanged:Connect(function()
local CurrentHealth = char.Humanoid.Health
local MaxHealth = char.Humanoid.MaxHealth
healthtext.Text = CurrentHealth.." / "..MaxHealth -- quite possible that current health will end up as an ugly decimal here, like 11.3484729293 or whatever. you might want to round currentHealth
bar.Size = UDim2.new(CurrentHealth / MaxHealth, 0, 1, 0)
end)
char:WaitForChild("Humanoid").HealthChanged:Connect(function() this line right here will ensure the code below it will fire whenever the Humanoids health changes.
Yes, so basically because that we have the .HealthChanged event running, it should be automatically “looped” per say. Are you having issues with the code currently?