How to detect when a humanoid has died

I’m trying to make an effect that happens when your health is low, but if you reset or die, the visual effect is still there and it ruins gameplay. I’ve tried using the official Roblox article on humanoid.died, and I added it, yet it still doesn’t work.

Here is the code

local character = script.Parent
local humanoid = character:WaitForChild('Humanoid')
local Players = game:GetService("Players")
local LowHealthBlur = game.Lighting.LowHealthEffects:WaitForChild("LowHealthBlur")
local LowHealthSat = game.Lighting.LowHealthEffects:WaitForChild("LowHealthSat")

local sound = script:WaitForChild("heartbeat") -- you may use any sound

humanoid.HealthChanged:connect(function(currentHealth)
	if humanoid.Health <= 37 then
		sound:Play()
		LowHealthBlur.Parent = game.Lighting
		LowHealthSat.Parent = game.Lighting
		--game.Lighting.LowHealthEffects.LowHealthBlur.Parent = game.Lighting
		--game.Lighting.LowHealthEffects.LowHealthSat.Parent = game.Lighting
		task.wait()
		LowHealthBlur.Enabled = true
		LowHealthSat.Enabled = true
		--game.Lighting.LowHealthBlur.Enabled = true
		--game.Lighting.LowHealthSat.Enabled = true
	elseif humanoid.Health > 37 or humanoid.Health <= 0 and sound.IsPlaying == true then
		sound:Stop()
		LowHealthBlur.Enabled = false
		LowHealthSat.Enabled = false
		--game.Lighting.LowHealthBlur.Enabled = false
		--game.Lighting.LowHealthSat.Enabled = false
		task.wait()
		LowHealthBlur.Parent = game.Lighting.LowHealthEffects
		LowHealthSat.Parent = game.Lighting.LowHealthEffects
		--game.Lighting.LowHealthBlur.Parent = game.Lighting.LowHealthEffects
		--game.Lighting.LowHealthSat.Parent = game.Lighting.LowHealthEffects
	elseif humanoid.Health <= 0 and currentHealth <= 0 and sound.IsPlaying == true then
		sound:Stop()
		LowHealthBlur.Enabled = false
		LowHealthSat.Enabled = false
		--game.Lighting.LowHealthBlur.Enabled = false
		--game.Lighting.LowHealthSat.Enabled = false
		task.wait()
		LowHealthBlur.Parent = game.Lighting.LowHealthEffects
		LowHealthSat.Parent = game.Lighting.LowHealthEffects
		--game.Lighting.LowHealthBlur.Parent = game.Lighting.LowHealthEffects
		--game.Lighting.LowHealthSat.Parent = game.Lighting.LowHealthEffects
	end	
end)

local function onPlayerAdded(plr)
	local function onCharacterAdded(character)
		local jj = character:WaitForChild("Humanoid")

		local function onDied()
			sound:Stop()
			LowHealthBlur.Enabled = false
			LowHealthSat.Enabled = false
			--game.Lighting.LowHealthBlur.Enabled = false
			--game.Lighting.LowHealthSat.Enabled = false
			LowHealthBlur.Parent = game.Lighting.LowHealthEffects
			LowHealthSat.Parent = game.Lighting.LowHealthEffects
			--game.Lighting.LowHealthBlur.Parent = game.Lighting.LowHealthEffects
			--game.Lighting.LowHealthSat.Parent = game.Lighting.LowHealthEffects
		end
		task.wait()
		jj.Died:Connect(onDied)
		print("gbgggg")
	end

	plr.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

first of all, you should put the code in functions if you’re going to use it more than once

local function enabledEffect()
	sound:Play()
	LowHealthBlur.Parent = game.Lighting
	LowHealthSat.Parent = game.Lighting
	--game.Lighting.LowHealthEffects.LowHealthBlur.Parent = game.Lighting
	--game.Lighting.LowHealthEffects.LowHealthSat.Parent = game.Lighting
	task.wait()
	LowHealthBlur.Enabled = true
	LowHealthSat.Enabled = true
	--game.Lighting.LowHealthBlur.Enabled = true
	--game.Lighting.LowHealthSat.Enabled = true
end

local function disabledEffect()
	sound:Stop()
	LowHealthBlur.Enabled = false
	LowHealthSat.Enabled = false
	--game.Lighting.LowHealthBlur.Enabled = false
	--game.Lighting.LowHealthSat.Enabled = false
	task.wait()
	LowHealthBlur.Parent = game.Lighting.LowHealthEffects
	LowHealthSat.Parent = game.Lighting.LowHealthEffects
	--game.Lighting.LowHealthBlur.Parent = game.Lighting.LowHealthEffects
	--game.Lighting.LowHealthSat.Parent = game.Lighting.LowHealthEffects
end

if this script is in StarterCharacterScripts, don’t use Players.PlayerAdded, just do this:

local character = script.Parent
local humanoid = character:WaitForChild('Humanoid')
local Players = game:GetService("Players")
local LowHealthBlur = game.Lighting.LowHealthEffects:WaitForChild("LowHealthBlur")
local LowHealthSat = game.Lighting.LowHealthEffects:WaitForChild("LowHealthSat")

local sound = script:WaitForChild("heartbeat") -- you may use any sound

local function enableEffect()
	sound:Play()
	LowHealthBlur.Parent = game.Lighting
	LowHealthSat.Parent = game.Lighting
	--game.Lighting.LowHealthEffects.LowHealthBlur.Parent = game.Lighting
	--game.Lighting.LowHealthEffects.LowHealthSat.Parent = game.Lighting
	task.wait()
	LowHealthBlur.Enabled = true
	LowHealthSat.Enabled = true
	--game.Lighting.LowHealthBlur.Enabled = true
	--game.Lighting.LowHealthSat.Enabled = true
end

local function disableEffect()
	sound:Stop()
	LowHealthBlur.Enabled = false
	LowHealthSat.Enabled = false
	--game.Lighting.LowHealthBlur.Enabled = false
	--game.Lighting.LowHealthSat.Enabled = false
	task.wait()
	LowHealthBlur.Parent = game.Lighting.LowHealthEffects
	LowHealthSat.Parent = game.Lighting.LowHealthEffects
	--game.Lighting.LowHealthBlur.Parent = game.Lighting.LowHealthEffects
	--game.Lighting.LowHealthSat.Parent = game.Lighting.LowHealthEffects
end

disableEffect()

humanoid.HealthChanged:connect(function(currentHealth)
	if humanoid.Health <= 37 and humanoid.Health > 0 then
		enableEffect()
	elseif humanoid.Health > 37 or humanoid.Health <= 0 and sound.IsPlaying == true then
		disableEffect()
	elseif humanoid.Health <= 0 and currentHealth <= 0 and sound.IsPlaying == true then
		disableEffect()
	end	
end)

also, what’s the point of 2 elseifs that are pretty much identical?

1 Like

It was for testing purposes to see if any of them would work

just check if humanoid has 0 or less hp bro

1 Like

Im sure there is a humand.died function?

1 Like

Did you read the post? He said that he tried using the Humanoid.Died() but he couldn’t get it to work. The first replier solved the issue in his post

1 Like

Man im blind

1 Like

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