How would I detect a player losing health for my healthbar?

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)

		local PlayerTag = script.Parent:WaitForChild("NameTag")
		local healthbar = PlayerTag.Backing:WaitForChild("HealthBar")
		local barhealth = healthbar:WaitForChild("BarBacking")
		local bar = barhealth:WaitForChild("Bar")
		local healthtext = barhealth:WaitForChild("HealthText")
		local playerhealth = Player.Character.Humanoid.Health
		local maxhealth = Player.Character.Humanoid.MaxHealth
		local parent = script.Parent.Parent
		local plrchar = Player:FindFirstChild("Character")
		local humanoidchar = Player.Character.Humanoid
		local humanoid = Player.Character.Humanoid
		local plr = game.Players.LocalPlayer
		local hum = Player.Character.Humanoid
		local OldHealth = hum.Health

		local plr = game.Players.LocalPlayer
		local char = Player.Character or Player.CharacterAdded:Wait()
		local hum = char:WaitForChild("Humanoid")

		healthtext.Text = humanoid.Health .."/".. humanoid.MaxHealth

		hum.HealthChanged:Connect(function()
			local CurrentHealth = humanoid.Health
			local MaxHealth = humanoid.MaxHealth
			healthtext.Text = CurrentHealth .."/".. MaxHealth
			bar.Size = UDim2.new(CurrentHealth / MaxHealth, 0, 1, 0)
repeat until nil
		end)
	end)
end)
			
	

This is my script, anyway I could fix it without using repeat until nil?

1 Like

I don’t understand why you are even using a repeat in the first place, the Humanoid.HealthChanged will fire every time the health is changed, so there is no need to loop it.
So all you need to do to make it without the “repeat until nil” is simply removing the “repeat until nil”.

Also… why do you define SO MANY variables, many of them being exactly the same thing…
You define “humanoid” once and “hum” twice. And you also get the player from the PlayerAdded function, so why do you set the player using game.Players.LocalPlayer?

4 Likes

With the Humanoid.Healthchanged, it doesn’t even work, it just stays as 100/100 the whole time

Not sure why it isn’t working, but it isn’t surprising because of all the random variables that you are using.
Please just use this code and build upon it to achieve what you want:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local hum = Character:WaitForChild("Humanoid")

		hum.HealthChanged:Connect(function()
			local CurrentHealth = hum.Health
			local MaxHealth = hum.MaxHealth
			
			print(CurrentHealth .."/".. MaxHealth)
		end)
	end)
end)

This code will print the remaining health every time the health changes.

1 Like

3 Ways:

1st way:

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(Character)
		local hum = Character:WaitForChild("Humanoid")

		hum.HealthChanged:Connect(function()
			--do stuff here
		end)
	end)
end)

2nd way:

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(Character)
		local hum = Character:WaitForChild("Humanoid")

		hum:GetPropertyChangedSignal("Health"):Connect(function()
			--do stuff here
		end)
	end)
end)

3d way:

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(Character)
		local hum = Character:WaitForChild("Humanoid")

		hum.Health.Changed:Connect(function()
			--do stuff here
		end)
	end)
end)
2 Likes

Tried all 3, none work, from before

Also something important to note when testing: Make sure when you change the health of the humanoid, you change it on the server. Opening the explorer and changing the health of humanoid down from 100 will not work.

1 Like

Is your script a local script? Also, the 3 options I gave you, should logically work fine. There is possibly a different issue.

It is a serverscript in ServerScriptService

You can’t get the player from the server like that, you dont need this line.
Using PlayerAdded event [with a Player parameter] will give you the player.

1 Like
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)

		local PlayerTag = script.Parent:WaitForChild("NameTag")
		local healthbar = PlayerTag.Backing:WaitForChild("HealthBar")
		local barhealth = healthbar:WaitForChild("BarBacking")
		local bar = barhealth:WaitForChild("Bar")
		local healthtext = barhealth:WaitForChild("HealthText")
		local playerhealth = Character.Humanoid.Health
		local maxhealth = Character.Humanoid.MaxHealth
		local parent = script.Parent.Parent
		local hum =Character.Humanoid
		local OldHealth = hum.Health
		local char = Character or Player.CharacterAdded:Wait()
		

		healthtext.Text = hum.Health .."/".. hum.MaxHealth
		hum.HealthChanged:Connect(function()
			local CurrentHealth = Character.Humanoid.Health
			local MaxHealth = Character.Humanoid.MaxHealth
			healthtext.Text = CurrentHealth .."/".. MaxHealth
			bar.Size = UDim2.new(CurrentHealth / MaxHealth, 0, 1, 0)
                      
		end)
	end)
end)

Why are you using a server script for a health bar GUI?

I noticed you also assigned a couple of Humanoid variables, why? You could just have one
Also, this line :

local plrchar = Player:FindFirstChild("Character") doesnt need to be there

1 Like

If you’re using a UI , and want it to update from server, this is totally fine.
[Alternatively you could use a remote from client to server - if you wanted the UI to be updated and seen for everyone].

1 Like

– localscript

humanoid:GetPropertyChangedSignal("Health"):Connect(function()
    bar.Size = UDim2.new(health / maxhealth, 0, 1, 0)
end)
1 Like

i am not sure what working but there is like a thing called
GetPropertyChangedSignal and it looks like this

hum:GetPropertyChangedSignal("Health"):Connect(function()
			local CurrentHealth = humanoid.Health
			local MaxHealth = humanoid.MaxHealth
			healthtext.Text = CurrentHealth .."/".. MaxHealth
			bar.Size = UDim2.new(CurrentHealth / MaxHealth, 0, 1, 0)
end)

so as i said idk whats the problem but yeah maybe its smth with HealthChanged cuz other code looks fine exept this repeat until nil out of nothing and a ton of useless variables

are you reading my mind dude lol

no, since i have experience with healthbars i just typed one up lol

you did at the same time as me and we mentioned the same getpropertychangedsignal so that is neat i guess

2 Likes

This isn’t serversided, this is client sided. How would I make this server side?

1 Like