Humanoid.Died Stops working after Character respawns

Title. Any help is appreciated!

Local Script (In StarterCharacters)

local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character

local TS = game:GetService("TweenService")
local TweenInfo1 = TweenInfo.new(6)

local DeathGui = plr.PlayerGui.DeathGui

local Event = game.ReplicatedStorage.RemoteEvents.PlayerDied

local Human = plr.Character:WaitForChild("Humanoid")

Human.Died:Connect(function()
	plr:SetAttribute("Dead", true)	
	TS:Create(DeathGui.DeathFrame, TweenInfo1, {BackgroundTransparency = 0}):Play()
	
	task.wait(6.5)
	
	DeathGui.DeathFrame.RestartButton.TextTransparency = 0
	DeathGui.DeathFrame.RestartButton.Active = true
	DeathGui.DeathFrame.RestartButton.MouseButton1Click:Connect(function()
		Event:FireServer(plr, Char)
		print("done")
		local HumRootPart = Human.Parent:WaitForChild("HumanoidRootPart")
		print("FOUND")

		local SpawnPoints = {
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart1"), 
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart2"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart3"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart4"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart5"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart6")
		}

		local ChosenSpawnNumber = math.random(1, #SpawnPoints)
		print(ChosenSpawnNumber)
		local ChosenSpawn = SpawnPoints[ChosenSpawnNumber]
		print(ChosenSpawn)
		game:GetService("Players").LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = ChosenSpawn.CFrame
	end)
end)

Server Script (In ServerScriptService)

local Players = game:GetService("Players")

local Event = game.ReplicatedStorage.RemoteEvents.PlayerDied
	
Event.OnServerEvent:Connect(function(plr, Char)
	plr:LoadCharacter()
	plr:SetAttribute("Dead", false)
	plr:SetAttribute("Sanity", 100)
	plr:SetAttribute("Insane", false)
	plr:SetAttribute("Stamina", 100)
	plr:SetAttribute("IsSprinting", false)
	plr:SetAttribute("CantSprint", false)
	Char:WaitForChild("Health"):Destroy()
end)

You’ll have to get the character once again when humanoid is dead since it will be replaced with a brand new character

--
--
local DeathGui = plr.PlayerGui.DeathGui
local Event = game.ReplicatedStorage.RemoteEvents.PlayerDied

local function onCharacterAdded(Char)
	local Human = Char:WaitForChild("Humanoid")
	Human.Died:Connect(function()
--
--
end

plr.CharacterAdded:Connect(onCharacterAdded)
local plr = game:GetService("Players").LocalPlayer
local Char = plr.Character or plr.CharacterAdded:Wait()

local TS = game:GetService("TweenService")
local TweenInfo1 = TweenInfo.new(6)
local connection 
local DeathGui = plr.PlayerGui.DeathGui

local Event = game.ReplicatedStorage.RemoteEvents.PlayerDied

local Human = Char:WaitForChild("Humanoid")

local function Died()
	plr:SetAttribute("Dead", true)	
	TS:Create(DeathGui.DeathFrame, TweenInfo1, {BackgroundTransparency = 0}):Play()

	task.wait(6.5)

	DeathGui.DeathFrame.RestartButton.TextTransparency = 0
	DeathGui.DeathFrame.RestartButton.Active = true
	DeathGui.DeathFrame.RestartButton.MouseButton1Click:Connect(function()
		Event:FireServer(plr, Char)
		print("done")
		local HumRootPart = Human.Parent:WaitForChild("HumanoidRootPart")
		print("FOUND")

		local SpawnPoints = {
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart1"), 
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart2"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart3"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart4"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart5"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart6")
		}

		local ChosenSpawnNumber = math.random(1, #SpawnPoints)
		print(ChosenSpawnNumber)
		local ChosenSpawn = SpawnPoints[ChosenSpawnNumber]
		print(ChosenSpawn)
		game:GetService("Players").LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame = ChosenSpawn.CFrame
	end)
	if connection then connection:Disconnect() end
	Char = plr.CharacterAdded:Wait()
	Human = Char:WaitForChild("Humanoid")
	connection = Human.Died:Connect(Died)
end

connection = Human.Died:Connect(Died)

This might work

Make sure to have the local script inside of the StarterCharacterScripts
folder instead of the StarterPlayerScripts folder. This is, because of when the player respawns, a new character object is created, with a new humanoid object in it. Therefore, the Human.Died event won’t run again, as the old humanoid doesn’t exist anymore. Hope this will work!

1 Like

You didn’t specify what stops working. The whole code inside the Humanoid.Died event? Or a section of it?

you need to wrap the Humanoid.Died() event inside of a CharacterAdded event, this makes it so that whenever a new Character is added (or when you respawn) it connects the Died event for that new Character:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)

		local humanoid = character:FindFirstChild("Humanoid")
		humanoid.Died:Connect(function()
			-- do the code you want to run when a player dies here

		end)
	end)
end)
1 Like

Sorry this didn’t work, am pretty sure characteradded doesnt work on local scripts

this also didnt work, thanks for trying

well it still runs after the character respawns but it doesnt teleport me to a spawnpoint so after the event fired

it didnt work, thanks for trying

How about this, instead of teleporting inside of the Humanoid.Died, how about you do it when the character respawns?

-- at the top of your script
if plr:GetAttribute("Respawning") == true then
   plr:SetAttribute("Respawning", false)
   local SpawnPoints = {
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart1"), 
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart2"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart3"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart4"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart5"),
			workspace.Level0Parts.SpawnParts:WaitForChild("SpawnPart6")
		}

		local ChosenSpawnNumber = math.random(1, #SpawnPoints)
		print(ChosenSpawnNumber)
		local ChosenSpawn = SpawnPoints[ChosenSpawnNumber]
		print(ChosenSpawn)
		plr.Character:WaitForChild("HumanoidRootPart").CFrame = ChosenSpawn.CFrame
end

Human.Died:Connect(function()
   plr:SetAttribute("Respawning", true)
   -- rest of your code besides the spawn point section
end)

EDIT: Explanation, when the player dies it sets an attribute (needed only on the client since that’s where the teleporting is happening unless you wanna move it on the server) which we then use on the next spawning of the player to know whether it’s a respawn or if the player has first spawned in. If the attribute is there then it’s a respawn and we teleport the player to a random spawn point, otherwise we ignore that section and let only the rest of the code run

1 Like

this works, thank you! (charlimit)

1 Like

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