How would I say when a player loses health?

Where is your script located?

If you wanted it to show your health right on spawn, it might be a good idea to put something above the event which will fire essentially right when the script is loaded up. For example:


local Player = game.Players.LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")
local healthtext = -- wherever healthtext is
local bar = -- wherever bar is


--whatever is here will run right on startup
healthtext.Text = Humanoid.Health.." / ".. Humanoid.MaxHealth

-- this is the event, whatever is in this codeblock will only happen whenever the health is changed
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)

ServerScriptService, it is a part of a big script that functions a whole nametag.

Oh, I was under the assumption that this was simply a LocalScript in StarterCharacter or something. Well, one way you could go about this is wrapping this in a CharacterAdded, like so:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char:WaitForChild("Humanoid")
		
		healthtext.Text = Humanoid.Health.." / ".. Humanoid.MaxHealth
		
		hum.HealthChanged:Connect(function()
			local CurrentHealth = hum.Health
			local MaxHealth = hum.MaxHealth
			healthtext.Text = CurrentHealth.." / "..MaxHealth
			bar.Size = UDim2.new(CurrentHealth / MaxHealth, 0, 1, 0)
			
		end)
	end)
end)
1 Like

If I change my health from the explorer humanoid, should the healthbar text change

If you’re doing it on the server, yes. Otherwise, no. Because this is a serverScript, it will be unable to process changes on the client. If you were to go to “Test” in a test session, then press on “Current: Client” , you can edit the change on the server. Once you go back onto the client, the changes will be shown there.

1 Like

I have inserted an attack dummy and when it damages me, the healthbar stays idle.

Could you show me what bar and healthText are in your script?

1 Like

Bar is a frame, healthtext is a textlabel.

		local bar = barhealth:WaitForChild("Bar")
		local healthtext = barhealth:WaitForChild("HealthText")

Is this referring PlayerGui, or StarterGui though?

It’s under a BillboardGUI, it is an overhead nametag with a healthbar

This healthbar does not work, seeking help.

Add a variable to define the last amount of health your Humanoid has in the HealthChanged function so we can determine if the Humanoid has taken damage or not.

Do something like this:

local Humanoid = Player.Character.Humanoid
local LastHealth = Humanoid.Health

Humanoid.HealthChanged:Connect(function(h)
	if h < LastHealth then
		print("Player has taken damage!")
	end
	
	LastHealth = h -- Remember to update the LastHealth value so that if (for example) the player's health was to increase, and then to decrease from taking damage, it will correctly determine if the Player has taken damage.
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.