-- Put In The StarterCharacterScripts
-- Fix Ragdoll CanCollide Off


local character = script.Parent

function recurse(root,callback,i)
	i= i or 0
	for _,v in pairs(root:GetChildren()) do
		i = i + 10
		callback(i,v)
		
		if #v:GetChildren() > 0 then
			i = recurse(v,callback,i)
		end
	end
	return i
end

function ragdollJoint(part0, part1, attachmentName, className, properties)
	attachmentName = attachmentName.."RigAttachment"
	local constraint = Instance.new(className.."Constraint")
	constraint.Attachment0 = part0:FindFirstChild(attachmentName)
	constraint.Attachment1 = part1:FindFirstChild(attachmentName)
	constraint.Name = "RagdollConstraint"..part1.Name
	
	for _,propertyData in next,properties or {} do
		constraint[propertyData[1]] = propertyData[2]
	end
	
	constraint.Parent = character
end

function getAttachment0(attachmentName)
	for _,child in next,character:GetChildren() do
		local attachment = child:FindFirstChild(attachmentName)
		if attachment then
			return attachment
		end
	end
end
character:WaitForChild("Humanoid").FreeFalling:connect(function()
character.StartRagdoll.Disabled = true
	--Make it so ragdoll can't collide with invisible HRP, but don't let HRP fall through map and be destroyed in process	
	character.UpperTorso.CanCollide = false
	character.Head.CanCollide = true
	character.HumanoidRootPart.CanCollide = false
	character.LeftLowerLeg.CanCollide = true
	character.RightLowerLeg.CanCollide = true
	character.LeftFoot.CanCollide = true
	character.RightFoot.CanCollide = true
local UTJoint = character.UpperTorso:FindFirstChild("WaistRigAttachment")
local LTJoint = character.LowerTorso:FindFirstChild("WaistRigAttachment")