Ragdoll stops working once geting close

I am trying to make that npc deaths would be ragdoll. The problem is that once npc dies he turns ragdoll but once i get close to npc the npc disables the ragdoll thats how i can explain it.
robloxapp-20201116-2114192.wmv (2.8 MB)
Here is the script
`local died
local character = script.Parent
local hum = character.Humanoid
character.Humanoid.BreakJointsOnDeath = false
died = character.Humanoid.Died:Connect(function()
for _, v in pairs(character:GetDescendants()) do
if v:IsA(“Motor6D”) then
local a0, a1 = Instance.new(“Attachment”), Instance.new(“Attachment”)
a0.CFrame = v.C0
a1.CFrame = v.C1
a0.Parent = v.Part0
a1.Parent = v.Part1

		local b = Instance.new("BallSocketConstraint")
		b.Attachment0 = a0
		b.Attachment1 = a1
		
		b.Parent = v.Part0
		
		v:Destroy()
	end
end
character.HumanoidRootPart.CanCollide = false

end)`
I hope you can help me.