Why isn't This Respawn Code Working?

local Player = game.Players.LocalPlayer
local DeathPosition

spawn(function()
    while wait(1) do
        DeathPosition = Player.Character:WaitForChild("HumanoidRootPart").CFrame
    end
end) 


Player.Character:FindFirstChildOfClass('Humanoid').Died:Connect(function()
    wait(5)
    Player.Character.HumanoidRootPart.CFrame = DeathPosition
end)

What is exactly not working?
Are you trying to teleport humanoidrootpart when player dies?

sorry i had the script in the wrong location i fixed it now

Instead of doing this on the client, why don’t you do this on the server?
Just put a script in ServerScriptService and put this:

--//Services
local Players = game:GetService("Players")

--//Functions
Players.PlayerAdded:Connect(function(player)
	local deathPivot = nil
	
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")
		
		Humanoid.Died:Connect(function()
			deathPivot = character:GetPivot()
		end)
		
		if deathPivot then			
			task.defer(function()
				character:PivotTo(deathPivot)
			end)
		end
	end)
end)