Humanoid.Died doesn't work again after the player dies for the first time

  1. What do you want to achieve?
    To understand and fix the script relating to Humanoid.Died.

  2. What is the issue?
    After dying the first time, Humanoid.Died doesn’t function properly anymore.

This server script is located inside the ServerScriptService.

game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:Connect(function(plr)
	
	local deathinstance = Instance.new("IntValue")
	deathinstance.Name= ("Death")
	deathinstance.Parent = plr
	deathinstance.Value = 0
	
	plr:LoadCharacter()
	print("player added")
	plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
	local val = Instance.new("ObjectValue",workspace.AlivePlayers)
	val.Name = plr.Name
	val.Value = plr
	local hum = plr.Character:WaitForChild("Humanoid")
	
	hum.Died:connect(function()
		local vale = workspace.AlivePlayers:FindFirstChild(plr.Name)
		plr.Death.Value += 1
		
		plr.PlayerGui.DeathScreen.DeathImpact:Play()
		plr.PlayerGui.DeathScreen.DeathImpact2:Play()
		plr.PlayerGui.DeathScreen.DeathAmbience:Play()
		
		if plr.Death.Value == 1 then
			wait(2)
			plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
			plr.PlayerGui.DeathScreen.DeathImpact:Stop()
			plr.PlayerGui.DeathScreen.DeathImpact2:Stop()
			
			
			plr.PlayerGui.DeathScreen.RespawnImpact:Play()
			plr.PlayerGui.DeathScreen.Respawning.Visible = true
			plr.PlayerGui.DeathScreen.RespawningSound:Play()
			local randomtext = math.random(1,4)
			if randomtext == 1 then
				plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can't give up now."
			elseif randomtext == 2 then
				plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You have another chance."
			elseif randomtext == 3 then
				plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can do this."
			elseif randomtext == 4 then
				plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "There is still hope for you."
			end
			
			wait(2)
			
			for i = 1,10 do
				plr.PlayerGui.DeathScreen.Respawning.RespawnText.TextTransparency -= 0.1
				wait()
			end
			
			wait(3.5)
			
			plr:LoadCharacter()
		else
			print("remove")
			vale:Destroy()
		end
		return
	end)
end)
  1. What solutions have you tried so far?
  • I have tried adding a CharacterAdded function, but it breaks the script even more so.
  • I have tried looking on the developer forums, but couldn’t find a solution. :frowning:

Thank you for taking the time to read this post! Have a nice day! :heart:

1 Like

Hey there PerfectGamer.

To understand what’s going on here, it’s important to visualize this from a character perspetive. Each time a character resets or a humanoid dies, a new one is created. This means that your player added connection up top only tracks the first ever character. Let’s fix that by wrapping the hum.Died:Connect() with a plr.CharacterAdded:Connect() signal. It would look something like this:

game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
        hum.Died:Connect(function()
            -- code here
        end)
    end)
end)

Keep in mind that these wraps can be functioned and sorted much neater. This is just an example to get yours working.

Let me know if everything goes well.

2 Likes

Hey Beetee! Thank you for helping me out!
Unfortunately, when I added the CharacterAdded function below the PlayerAdded function, the character never loaded in, so I moved plr:LoadCharacter() in between both functions. The character now loads in, but whenever the player dies, the whole script doesn’t work. :frowning:

This is what I have now currently:

game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:Connect(function(plr)
	plr:LoadCharacter()
	plr.CharacterAdded:Connect(function(character)

		local deathinstance = Instance.new("IntValue")
		deathinstance.Name= ("Death")
		deathinstance.Parent = plr
		deathinstance.Value = 0


		print("player added")
		plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
		local val = Instance.new("ObjectValue",workspace.AlivePlayers)
		val.Name = plr.Name
		val.Value = plr
		local hum = plr.Character:WaitForChild("Humanoid")

		hum.Died:connect(function()
			local vale = workspace.AlivePlayers:FindFirstChild(plr.Name)
			plr.Death.Value += 1

			plr.PlayerGui.DeathScreen.DeathImpact:Play()
			plr.PlayerGui.DeathScreen.DeathImpact2:Play()
			plr.PlayerGui.DeathScreen.DeathAmbience:Play()

			if plr.Death.Value == 1 then
				wait(2)
				plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
				plr.PlayerGui.DeathScreen.DeathImpact:Stop()
				plr.PlayerGui.DeathScreen.DeathImpact2:Stop()


				plr.PlayerGui.DeathScreen.RespawnImpact:Play()
				plr.PlayerGui.DeathScreen.Respawning.Visible = true
				plr.PlayerGui.DeathScreen.RespawningSound:Play()
				local randomtext = math.random(1,4)
				if randomtext == 1 then
					plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can't give up now."
				elseif randomtext == 2 then
					plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You have another chance."
				elseif randomtext == 3 then
					plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can do this."
				elseif randomtext == 4 then
					plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "There is still hope for you."
				end

				wait(2)

				for i = 1,10 do
					plr.PlayerGui.DeathScreen.Respawning.RespawnText.TextTransparency -= 0.1
					wait()
				end

				wait(3.5)

				plr:LoadCharacter()
			else
				print("remove")
				vale:Destroy()
			end
			return
		end)
	end)
end)
1 Like

You would have to set the :LoadCharacter() after the CharacterAdded connection. That should do the trick.

1 Like

Setting the :LoadCharacter() after the CharacterAdded function doesn’t properly load the character in. The player is added, but not the character itself.

1 Like

I see the issue. You are doing :WaitForChild() to get the humanoid of the character. However, the character has yet to load, which causes infinite yield. Go ahead and wrap everything inside CharacterAdded in a

task.spawn(function() 
     -- code here 
end)
1 Like

Hey Beetee! I tried adding the spawn function into the script, but the player still doesn’t load unfortunately. :frowning:

game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(character)
		task.spawn(function() 
			local deathinstance = Instance.new("IntValue")
			deathinstance.Name= ("Death")
			deathinstance.Parent = plr
			deathinstance.Value = 0

			print("player added")
			plr:LoadCharacter()
			plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
			local val = Instance.new("ObjectValue",workspace.AlivePlayers)
			val.Name = plr.Name
			val.Value = plr
			local hum = plr.Character:WaitForChild("Humanoid")

			hum.Died:connect(function()
				local vale = workspace.AlivePlayers:FindFirstChild(plr.Name)
				plr.Death.Value += 1

				plr.PlayerGui.DeathScreen.DeathImpact:Play()
				plr.PlayerGui.DeathScreen.DeathImpact2:Play()
				plr.PlayerGui.DeathScreen.DeathAmbience:Play()

				if plr.Death.Value == 1 then
					wait(2)
					plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
					plr.PlayerGui.DeathScreen.DeathImpact:Stop()
					plr.PlayerGui.DeathScreen.DeathImpact2:Stop()


					plr.PlayerGui.DeathScreen.RespawnImpact:Play()
					plr.PlayerGui.DeathScreen.Respawning.Visible = true
					plr.PlayerGui.DeathScreen.RespawningSound:Play()
					local randomtext = math.random(1,4)
					if randomtext == 1 then
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can't give up now."
					elseif randomtext == 2 then
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You have another chance."
					elseif randomtext == 3 then
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can do this."
					elseif randomtext == 4 then
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "There is still hope for you."
					end

					wait(2)

					for i = 1,10 do
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.TextTransparency -= 0.1
						wait()
					end

					wait(3.5)

					plr:LoadCharacter()
				else
					print("remove")
					vale:Destroy()
				end
			end)
		end)
	end)
end)
1 Like

You still have your LoadCharacter() inside the CharacterAdded. It has to be outside it, way way down below.

game.Players.PlayerAdded:Connect(function(plr)
     plr.CharacterAdded:Connect(function(character)
          -- code
     end)
     plr:LoadCharacter()
end)
2 Likes

Hey beetee!
After reworking my script similar to yours, it finally works! Thank you so much! :smile:

The script now:

game.Players.CharacterAutoLoads = false

game.Players.PlayerAdded:Connect(function(plr)
	print("player added")
	plr.CharacterAdded:Connect(function(character)
		--task.spawn(function() 
		repeat wait() until plr.Character:WaitForChild("Humanoid")
			local deathinstance = Instance.new("IntValue")
			deathinstance.Name= ("Death")
			deathinstance.Parent = plr
			deathinstance.Value = 0

			print("character added")

			plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
			local val = Instance.new("ObjectValue",workspace.AlivePlayers)
			val.Name = plr.Name
			val.Value = plr
			local hum = plr.Character:WaitForChild("Humanoid")
			print("remove")
		

			hum.Died:connect(function()
				local vale = workspace.AlivePlayers:FindFirstChild(plr.Name)
				plr.Death.Value += 1

				plr.PlayerGui.DeathScreen.DeathImpact:Play()
				plr.PlayerGui.DeathScreen.DeathImpact2:Play()
			plr.PlayerGui.DeathScreen.DeathAmbience:Play()
			
			vale:Destroy()

				if plr.Death.Value == 1 then
					wait(2)
					plr.PlayerGui.DeathScreen.DeathAmbience:Stop()
					plr.PlayerGui.DeathScreen.DeathImpact:Stop()
					plr.PlayerGui.DeathScreen.DeathImpact2:Stop()


					plr.PlayerGui.DeathScreen.RespawnImpact:Play()
					plr.PlayerGui.DeathScreen.Respawning.Visible = true
					plr.PlayerGui.DeathScreen.RespawningSound:Play()
					local randomtext = math.random(1,4)
					if randomtext == 1 then
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can't give up now."
					elseif randomtext == 2 then
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You have another chance."
					elseif randomtext == 3 then
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "You can do this."
					elseif randomtext == 4 then
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.Text = "There is still hope for you."
					end

					wait(2)

					for i = 1,10 do
						plr.PlayerGui.DeathScreen.Respawning.RespawnText.TextTransparency -= 0.1
						wait()
					end

					wait(3.5)

					plr:LoadCharacter()

				end
			--end)
		end)
	end)
	plr:LoadCharacter()
end)
2 Likes

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