Health bar help

Making a health bar.

In theory, the bar should move when the health is changed but it does not.

Here is my code:

local serverStorage = game.ServerStorage

local healthBar = serverStorage.HealthBar
local health = healthBar.Health

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local healthBarClone = healthBar:Clone()
		healthBarClone.Parent = character:WaitForChild("Head")
		
		local humanoid = character.Humanoid
		local maxHealth = humanoid.MaxHealth
		
		humanoid.HealthChanged:Connect(function(currentHealth)
			health:TweenSize(UDim2.new(currentHealth / maxHealth, 0, 1, 0), "In", "Bounce")
		end)
	end)
end)

Help! :pray:

Are there any errors in the console?

By the way, this should be in the scripting support category.

1 Like
  1. There are no errors.
  2. I thought it would be code review because it is reviewing code, I will change it.
2 Likes

Try print debugging it and see if the HealthChanged event actually fires.

6 Likes

Try by simply doing:
health:TweenSize(UDim2.new(humanoid.Health / humanoid.MaxHealth, 0, 1, 0), "In", "Bounce")

1 Like

All 8 of these statements print…

print("1")

local serverStorage = game.ServerStorage

local healthBar = serverStorage.HealthBar
local health = healthBar.Health

print("2")

game.Players.PlayerAdded:Connect(function(player)
	
	print("3")
	
	player.CharacterAdded:Connect(function(character)
		
		print("4")
		
		local healthBarClone = healthBar:Clone()
		healthBarClone.Parent = character:WaitForChild("Head")
		
		print("5")

		local humanoid = character.Humanoid
		local maxHealth = humanoid.MaxHealth
		
		print("6")

		humanoid.HealthChanged:Connect(function(currentHealth)
			
			print("7")
			
			health:TweenSize(UDim2.new(currentHealth / maxHealth, 0, 1, 0), "In", "Bounce")
			
			print("8")
		end)
	end)
end)
1 Like

And this did not work, same outcome of it not moving.

1 Like

Maybe try filling in all the parameters:
health:TweenSize(UDim2.new(currentHealth / maxHealth, 0, 1, 0), Enum.EasingDirection.In, Enum.EasingStyle.Bounce, 0.5, true)
I don’t think much will change, but it’s always good to try. If it doesn’t work, I recommend moving the health bar on the client instead. Check the bar size on the server first, if the size is different than the client’s bar then the problem is that you’re doing it on the server.
I also recommend updating the humanoid’s maxhealth everytime the health changes
(maxHealth = humanoid.MaxHealth at each HealthChanged event).

3 Likes

Pretty sure the first parameter for Humanoid.HealthChanged is the damage, not the current Health. Not 100% sure though, don’t quote me on this.

1 Like

i have a health bar and the first parameter of Humanoid.HealthChanged is the new health

1 Like

Okay thanks, I wasn’t 100% sure

1 Like

The “health” variable you’re using is for the UI element located in ServerStorage. By calling this function:

health:TweenSize(UDim2.new(currentHealth / maxHealth, 0, 1, 0), "In", "Bounce")

you’re tweening the health element in ServerStorage.

You should try this instead:

local serverStorage = game.ServerStorage

local healthBar = serverStorage.HealthBar

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local healthBarClone = healthBar:Clone()
		healthBarClone.Parent = character:WaitForChild("Head")
		local health = healthBarClone.Health
		
		local humanoid = character.Humanoid
		local maxHealth = humanoid.MaxHealth
		
		humanoid.HealthChanged:Connect(function(currentHealth)
			health:TweenSize(UDim2.new(currentHealth / maxHealth, 0, 1, 0), "In", "Bounce")
		end)
	end)
end)
4 Likes

maybe try this? im not 100% sure this will work.

local serverStorage = game.ServerStorage

local healthBar = serverStorage.HealthBar
local health = healthBar.Health

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local healthBarClone = healthBar:Clone()
		healthBarClone.Parent = character:WaitForChild("Head")
		local health = healthBarClone.Health
		
		local humanoid = character.Humanoid
		local maxHealth = humanoid.MaxHealth

		humanoid.HealthChanged:Connect(function(currentHealth)
			health:TweenSize(UDim2.new(currentHealth / maxHealth, 0, 1, 0), "In", "Bounce")
		end)
	end)
end)
1 Like

Try doing this:

local serverStorage = game.ServerStorage

local healthBar = serverStorage.HealthBar
local health = healthBar.Health

function AddHealth(character)
    local healthBarClone = healthBar:Clone()
		healthBarClone.Parent = character:WaitForChild("Head")
		
		local humanoid = character.Humanoid
		local maxHealth = humanoid.MaxHealth
		
		humanoid.HealthChanged:Connect(function(currentHealth)
			health:TweenSize(UDim2.new(currentHealth / maxHealth, 0, 1, 0), "In", "Bounce")
	 end)
end

game.Players.PlayerAdded:Connect(function(player)
    local character = player.Character or player.CharacterAdded:Wait()
    AddHealth(character)

	player.CharacterAdded:Connect(AddHealth)
end)

The character may not have loaded yet, which is why it is not working.

1 Like

Also, a tip I have is to set the time to:

1 - (currentHealth / maxHealth)

This will make your health bar more consistent.

1 Like

@TheNewrestart is totally correct. Did you try what he suggested?

Thank you for that! characters gotta speak