How would I say when a player loses health?

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:

  • Character.Humanoid.HealthChanged:Connect(function()
  • Humanoid.Health.Changed:Connect(function()

Any help would be appreciated!

Code which is in a big script:

									if Player.Character.Humanoid.Health >= 1 then

										bar.Size = UDim2.new(playerhealth / maxhealth, 0, 1, 0)
										healthtext.Text = humanoid.Health .. "/" .. humanoid.MaxHealth

									elseif Player.Character.Humanoid.Health == 0 then

										healthtext.Text = "0/100"
										bar.Visible = false

									elseif Player.Character.Humanoid.Health >= 0.1 then

										healthtext.Text = humanoid.Health .. "/" .. humanoid.MaxHealth
										bar.Visible = true```
1 Like

These links might help:
https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged
https://developer.roblox.com/en-us/api-reference/property/Humanoid/Health

The first link has 2 examples of code near the bottom that would also help.

2 Likes

https://developer.roblox.com/en-us/api-reference/event/Humanoid/HealthChanged

Roblox actually has something for Humanoid Health changes. You could use this to detect when the health of a humanoid changes.

1 Like

Appreciate the help, will check out all posts!

Will check out the posts, because I have tried ALL those

It didn’t work? Hm, there may be an issue with the way your code is functioning then. Do you still have it?

1 Like

Yes, I am currently opening and sending it.

1 Like

Check the code, I have sent it.

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?

1 Like

Yes, I am trying to change the size of the health bar and the text whenever a player loses health.

The issue is that it says I cannot divide it. Its saying arithmatic trying to connect to RBXScriptloader.

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)

Kinda hard to type on mobile ngl

3 Likes

rip, thanks for helping. will try!

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)
2 Likes

Yeah I figured the / 0, 1, 0 couldve helped

is there a way to loop it? I can’t seem to find a way to.

Could you go more in depth on loop?

char:WaitForChild("Humanoid").HealthChanged:Connect(function() this line right here will ensure the code below it will fire whenever the Humanoids health changes.

Which means like, it won’t run once and stop. So it won’t just define the plrs health once and will keep running.

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?

1 Like

Yes, when I spawn it does not show my actual health, rather the placeholder. Also, when I die and respawn, it says 0/100 forever.