Script not detecting health change

  1. What do you want to achieve? A script that will print (dead) when the character’s humanoid health is at 0 (i dont want to kill then as they will be revived)

  2. What is the issue? the script doesn’t detect health change after i set the humanoids dead state to false.

  3. What solutions have you tried so far? I have tried using humanoid.HealthChanged and a while true do loop but neither one have worked

here’s the script

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)	
		if character:GetAttribute("Species") == "Heretic" then

			local humanoid = character.Humanoid
			character.Humanoid:SetStateEnabled("Dead", false)
			
			humanoid.HealthChanged:Connect(function(health)
				if health <= 0 then
					print("dead")
				end
			end)
		end
	end)
end)
4 Likes

You could use fake health perhaps.

Once a humanoid enters the dead state, it can’t be brought back, as the humanoid is ‘killed’ at that point.

char:GetAttributeChangedSignal("Health"):Connect(function()
local currentHealth = char:GetAttribute("Health")

if currentHealth <= 0 then
--//do stuff
end
end)
2 Likes

U need to define the event connection before you set the state to Enum.HumanoidStateType.Dead.

1 Like

Try this? It detects whener a Humanoid health’s reached 0 or dead


local players = game.Players

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		if humanoid then
			if humanoid:GetState() == Enum.HumanoidStateType.Dead then
				print("Dead")
			end
		end
	end)
end)
1 Like

This would work under normal circumstances, however in this instance, you want to register the character as “Dead”, but, you don’t want the actual character to die because you will respawn when you do.

1 Like

Oh, I thought she wants to print out " dead " when the character is litterally dead. Because of the statement she was targetting

1 Like

Btw, Why can’t she just set the Respawn Time in a large number, And Loadthecharacter once the targetting event was done. There is a lot of way to do this such as setting an Attribute and also the setting a value into a player to define as " Health "

1 Like

When your character respawns it respawns your original character, but what if before you died you had these int values or number values, or anything for that matter that are crucial to the game. Once you die you lose all those things, of course to solve that you can readd those but you will need to know what those values were before they died, so you can put them back to normal. So it would be much easier to fake “Died” instead of actually dying.

1 Like

You won’t lose it, Infact there is a way because of DataStores.

Yes, but when you die you would have to save it to the data store and when you load in you would have to get it from the data store, then simply you can avoid all the hassle of doing so.
Also, I believe DataStores can have a tendency to not save when you use them too often, and this may be a game where you die often.

1 Like

I’ve been having the same issue recently and it seems that the character is being added before CharacterAdded can be run. Here’s the edited version of your script that should work:

game.Players.PlayerAdded:Connect(function(player)
	if player.Character then
		local character = player.Character
		if character:GetAttribute("Species") == "Heretic" then

			local humanoid = character.Humanoid
			character.Humanoid:SetStateEnabled("Dead", false)
			
			humanoid.HealthChanged:Connect(function(health)
				if health <= 0 then
					print("dead")
				end
			end)
		end
	end

	player.CharacterAdded:Connect(function(character)	
		if character:GetAttribute("Species") == "Heretic" then

			local humanoid = character.Humanoid
			character.Humanoid:SetStateEnabled("Dead", false)
			
			humanoid.HealthChanged:Connect(function(health)
				if health <= 0 then
					print("dead")
				end
			end)
		end
	end)
end)

I’d also recommend using a function to handle this to avoid repeating code.

3 Likes