Character arms, legs and head get CanCollide set back to false after server set it to true in ragdoll script

Hello guys. Mostly, I work with custom rigs. But today I have found need to work with roblox ones, and make ragdoll on death… But it is unexpectedly hard to do for me. I have rescripted movement scripts, did custom ragdoll script - nothing helps. Character either levitates in air, or after 2-3 seconds of ragdoll “Unragdolls” himself in weird way:
image
Can anyone tell me, what’s wrong with my ragdoll script, and how I can fix it?

local Deaths = {
	Ragdoll = function(Character)
		local Motors = {}
		Character.HumanoidRootPart.Massless = true
		for _, Object in pairs(Character:GetChildren()) do
			if Object:IsA("BasePart") then
				Object.CanCollide = true
				for _, Motor in pairs(Object:GetChildren()) do
					if Motor:IsA("Motor6D") then
						local Collision = Instance.new("NoCollisionConstraint")
						Collision.Name = "Collider"
						Collision.Part0 = Motor.Part0
						Collision.Part1 = Motor.Part1
						local Att0 = Instance.new("Attachment")
						Att0.CFrame = Motor.C0
						Att0.Name = Motor.Part0.Name
						local Att1 = Instance.new("Attachment")
						Att1.CFrame = Motor.C1
						Att1.Name = Motor.Part1.Name
						Att0.Parent = Motor.Part0
						Att1.Parent = Motor.Part1
						if Motor.Part0 == Character.HumanoidRootPart then
							local Weld = Instance.new("WeldConstraint")
							Weld.Part0 = Motor.Part0
							Weld.Part1 = Motor.Part1
							Weld.Parent = Motor.Parent
							table.insert(Motors, Weld)
						else
							local Ball = Instance.new("BallSocketConstraint")
							Ball.Name = Motor.Name
							Ball.Name = Motor.Name
							Ball.LimitsEnabled = true
							Ball.UpperAngle = 45
							Ball.TwistLimitsEnabled = true
							Ball.TwistLowerAngle = -60
							Ball.TwistUpperAngle = 60
							Ball.Restitution = 0.2
							Ball.Attachment0 = Att0
							Ball.Attachment1 = Att1
							Ball.Parent = Motor.Parent
							table.insert(Motors, Ball)
						end
						Collision.Parent = Motor.Parent
						Motor.Enabled = false
					end
				end
			end
		end
		Character.HumanoidRootPart.CanCollide = false
	end
}

local DeathMeta = {}
DeathMeta.__index = DeathMeta
local DeathController = setmetatable({}, DeathMeta)

function DeathMeta.KillCharacter(DeathType, Character)
	Deaths[DeathType](Character)
end

return DeathController

The ragdoll issue may stem from errors in constraint management or interference from other scripts. Try this ModuleScript that might help:

local Deaths = {
	Ragdoll = function(Character)
		local Motors = {}
		Character:WaitForChild("HumanoidRootPart").Massless = true
		for _, Object in pairs(Character:GetChildren()) do
			if Object:IsA("BasePart") then
				Object.CanCollide = true
				for _, Motor in pairs(Object:GetChildren()) do
					if Motor:IsA("Motor6D") then
						local Collision = Instance.new("NoCollisionConstraint")
						Collision.Name = "Collider"
						Collision.Part0 = Motor.Part0
						Collision.Part1 = Motor.Part1
						local Att0 = Instance.new("Attachment")
						Att0.CFrame = Motor.C0
						Att0.Name = Motor.Part0.Name
						local Att1 = Instance.new("Attachment")
						Att1.CFrame = Motor.C1
						Att1.Name = Motor.Part1.Name
						Att0.Parent = Motor.Part0
						Att1.Parent = Motor.Part1
						if Motor.Part0 == Character:WaitForChild("HumanoidRootPart") then
							local Weld = Instance.new("WeldConstraint")
							Weld.Part0 = Motor.Part0
							Weld.Part1 = Motor.Part1
							Weld.Parent = Motor.Parent
							table.insert(Motors, Weld)
						else
							local Ball = Instance.new("BallSocketConstraint")
							Ball.Name = Motor.Name
							Ball.LimitsEnabled = true
							Ball.UpperAngle = 45
							Ball.TwistLimitsEnabled = true
							Ball.TwistLowerAngle = -60
							Ball.TwistUpperAngle = 60
							Ball.Restitution = 0.2
							Ball.Attachment0 = Att0
							Ball.Attachment1 = Att1
							Ball.Parent = Motor.Parent
							table.insert(Motors, Ball)
						end
						Collision.Parent = Motor.Parent
						Motor.Enabled = false
					end
				end
			end
		end
		Character:WaitForChild("HumanoidRootPart").CanCollide = false
	end
}

local DeathMeta = {}
DeathMeta.__index = DeathMeta
local DeathController = setmetatable({}, DeathMeta)

function DeathMeta.KillCharacter(DeathType, Character)
	Deaths[DeathType](Character)
end

return DeathController

Ehm, nothing changed?
And, why you replied with AI-response?

Haha, I’m far from using an AI, I’m just relying on some of my knowledge. I mainly thought the issue might stem from the HumanoidRootPart, as I’ve never dealt with ragdoll mechanics before. I just try to help u boy.