R6 Corpse System

Overview:
I want to improve this system, something about it to me feels off. Perhaps it isn’t smooth enough, or it is too bad. I would like some feedback from the community on perhaps what to add, remove or edit in the code. Feel free to give it a try yourself in Roblox Studio!

My main goal is to perfect this system, but i need some ideas aswell

local players = game:GetService("Players")
local debris = game:GetService("Debris")

local CORPSE_LIFETIME = 70

local function makeRagDoll(doll)
	for _, part in ipairs(doll:GetDescendants()) do
		if part:IsA("Motor6D") then
			local socket = Instance.new("BallSocketConstraint")
			local a1 = Instance.new("Attachment")
			local a2 = Instance.new("Attachment")

			a1.Parent = part.Part0
			a2.Parent = part.Part1
			socket.Parent = part.Parent
			socket.Attachment0 = a1
			socket.Attachment1 = a2

			a1.CFrame = part.C0
			a2.CFrame = part.C1
			socket.LimitsEnabled = true
			socket.TwistLimitsEnabled = true

			part:Destroy()
		end
	end
end

local function makeOriginalInvisible(char)
	for _, part in ipairs(char:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Transparency = 1
			part.CanCollide = false
		end
	end
end

local function handleDeath(char)
	char.Archivable = true
	local dChar = char:Clone()
	char.Archivable = false

	local humanoidRootPart = dChar:FindFirstChild("HumanoidRootPart")
	if humanoidRootPart then
		humanoidRootPart.Anchored = false
		humanoidRootPart:Destroy()
	end

	if dChar then
		dChar.Parent = workspace
		dChar:SetPrimaryPartCFrame(char:GetPrimaryPartCFrame())

		for _, part in ipairs(dChar:GetDescendants()) do
			if part:IsA("BasePart") then
				part.CanCollide = true
				part.Anchored = false
				part.Transparency = 0
			end
		end

		local humanoid = dChar:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.Health = 0
			humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
			humanoid:ChangeState(Enum.HumanoidStateType.Dead)
		end

		makeRagDoll(dChar)

		debris:AddItem(dChar, CORPSE_LIFETIME)
	else
		warn("Failed to clone character!")
	end

	makeOriginalInvisible(char)
end

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid")
		humanoid.BreakJointsOnDeath = false
		humanoid.Died:Connect(function()
			print("Player Has Died!", player.Name)
			handleDeath(char)
		end)
	end)
end)