local q=workspace.CurrentCamera if q.CameraSubject==character.Humanoid then q.CameraSubject=character.UpperTorso end character.HumanoidRootPart.Anchored=true character.HumanoidRootPart.CanCollide=false recurse(character,function(q,r)if r:IsA("\065\116\116\097\099\104\109\101\110\116")then r.Axis=Vector3.new(0,1,0)r.SecondaryAxis=Vector3.new(0,0,1)r.Rotation=Vector3.new(0,0,0)end end)for q,r in next,character:GetChildren()do if r:IsA("\065\099\099\111\117\116\114\101\109\101\110\116")then for q,r in next,r:GetChildren()do if r:IsA("\066\097\115\101\080\097\114\116")then local q=r:FindFirstChildOfClass("\065\116\116\097\099\104\109\101\110\116")local w=getAttachment0(q.Name)if w and q then local r=Instance.new("\072\105\110\103\101\067\111\110\115\116\114\097\105\110\116")r.Attachment0=w r.Attachment1=q r.LimitsEnabled=true r.UpperAngle=0 r.LowerAngle=0 r.Parent=character end end end end end ragdollJoint(character.LowerTorso,character.UpperTorso,"\087\097\105\115\116","\066\097\108\108\083\111\099\107\101\116",{{"\076\105\109\105\116\115\069\110\097\098\108\101\100",true},{"\085\112\112\101\114\065\110\103\108\101",5}})local r={{"\076\105\109\105\116\115\069\110\097\098\108\101\100";true};{"\085\112\112\101\114\065\110\103\108\101",0},{"\076\111\119\101\114\065\110\103\108\101";0}}ragdollJoint(character.LeftLowerArm,character.LeftHand,"\076\101\102\116\087\114\105\115\116","\072\105\110\103\101",r)ragdollJoint(character.RightLowerArm,character.RightHand,"\082\105\103\104\116\087\114\105\115\116","\072\105\110\103\101",r)local w={{"\076\105\109\105\116\115\069\110\097\098\108\101\100";true};{"\085\112\112\101\114\065\110\103\108\101",0};{"\076\111\119\101\114\065\110\103\108\101";-75}}ragdollJoint(character.LeftUpperLeg,character.LeftLowerLeg,"\076\101\102\116\075\110\101\101","\072\105\110\103\101",w)ragdollJoint(character.RightUpperLeg,character.RightLowerLeg,"\082\105\103\104\116\075\110\101\101","\072\105\110\103\101",w)local B={{"\076\105\109\105\116\115\069\110\097\098\108\101\100";true},{"\085\112\112\101\114\065\110\103\108\101";15},{"\076\111\119\101\114\065\110\103\108\101";-45}}ragdollJoint(character.LeftLowerLeg,character.LeftFoot,"\076\101\102\116\065\110\107\108\101","\072\105\110\103\101",B)ragdollJoint(character.RightLowerLeg,character.RightFoot,"\082\105\103\104\116\065\110\107\108\101","\072\105\110\103\101",B)ragdollJoint(character.UpperTorso,character.LeftUpperArm,"\076\101\102\116\083\104\111\117\108\100\101\114","\066\097\108\108\083\111\099\107\101\116")ragdollJoint(character.LeftUpperArm,character.LeftLowerArm,"\076\101\102\116\069\108\098\111\119","\066\097\108\108\083\111\099\107\101\116")ragdollJoint(character.UpperTorso,character.RightUpperArm,"\082\105\103\104\116\083\104\111\117\108\100\101\114","\066\097\108\108\083\111\099\107\101\116")ragdollJoint(character.RightUpperArm,character.RightLowerArm,"\082\105\103\104\116\069\108\098\111\119","\066\097\108\108\083\111\099\107\101\116")ragdollJoint(character.LowerTorso,character.LeftUpperLeg,"\076\101\102\116\072\105\112","\066\097\108\108\083\111\099\107\101\116")ragdollJoint(character.LowerTorso,character.RightUpperLeg,"\082\105\103\104\116\072\105\112","\066\097\108\108\083\111\099\107\101\116")
--character:WaitForChild("Humanoid").Died:connect(function()
local camera = workspace.CurrentCamera
if camera.CameraSubject == character.Humanoid then --If developer isn't controlling camera
	camera.CameraSubject = character.UpperTorso
end

--Make it so ragdoll can't collide with invisible HRP, but don't let HRP fall through map and be destroyed in process
character.HumanoidRootPart.Anchored = true
character.HumanoidRootPart.CanCollide = false

--Helps to fix constraint spasms
recurse(character, function(_, v)
	if v:IsA("Attachment") then
		v.Axis = Vector3.new(0, 1, 0)
		v.SecondaryAxis = Vector3.new(0, 0, 1)
		v.Rotation = Vector3.new(0, 0, 0)
	end
end)

--Re-attach hats
for _, child in next, character:GetChildren() do
	if child:IsA("Accoutrement") then
		--Loop through all parts instead of only checking for one to be forwards-compatible in the event
		--ROBLOX implements multi-part accessories
		for _, part in next, child:GetChildren() do
			if part:IsA("BasePart") then
				local attachment1 = part:FindFirstChildOfClass("Attachment")
				local attachment0 = getAttachment0(attachment1.Name)
				if attachment0 and attachment1 then
					--Shouldn't use constraints for this, but have to because of a ROBLOX idiosyncrasy where
					--joints connecting a character are perpetually deleted while the character is dead
					local constraint = Instance.new("HingeConstraint")
					constraint.Attachment0 = attachment0
					constraint.Attachment1 = attachment1
					constraint.LimitsEnabled = true
					constraint.UpperAngle = 0 --Simulate weld by making it difficult for constraint to move
					constraint.LowerAngle = 0
					constraint.Parent = character
				end
			end
		end
	end
end

ragdollJoint(character.LowerTorso, character.UpperTorso, "Waist", "BallSocket", {
	{ "LimitsEnabled", true },
	{ "UpperAngle", 5 },
})
--[[ragdollJoint(character.UpperTorso, character.Head, "Neck", "BallSocket", {
		{"LimitsEnabled",true};
		{"UpperAngle",15};
	})]]

local handProperties = {
	{ "LimitsEnabled", true },
	{ "UpperAngle", 0 },
	{ "LowerAngle", 0 },
}
ragdollJoint(character.LeftLowerArm, character.LeftHand, "LeftWrist", "Hinge", handProperties)
ragdollJoint(character.RightLowerArm, character.RightHand, "RightWrist", "Hinge", handProperties)

local shinProperties = {
	{ "LimitsEnabled", true },
	{ "UpperAngle", 0 },
	{ "LowerAngle", -75 },
}
ragdollJoint(character.LeftUpperLeg, character.LeftLowerLeg, "LeftKnee", "Hinge", shinProperties)
ragdollJoint(character.RightUpperLeg, character.RightLowerLeg, "RightKnee", "Hinge", shinProperties)

local footProperties = {
	{ "LimitsEnabled", true },
	{ "UpperAngle", 15 },
	{ "LowerAngle", -45 },
}
ragdollJoint(character.LeftLowerLeg, character.LeftFoot, "LeftAnkle", "Hinge", footProperties)
ragdollJoint(character.RightLowerLeg, character.RightFoot, "RightAnkle", "Hinge", footProperties)

--TODO fix ability for socket to turn backwards whenn ConeConstraints are shipped
ragdollJoint(character.UpperTorso, character.LeftUpperArm, "LeftShoulder", "BallSocket")
ragdollJoint(character.LeftUpperArm, character.LeftLowerArm, "LeftElbow", "BallSocket")
ragdollJoint(character.UpperTorso, character.RightUpperArm, "RightShoulder", "BallSocket")
ragdollJoint(character.RightUpperArm, character.RightLowerArm, "RightElbow", "BallSocket")
ragdollJoint(character.LowerTorso, character.LeftUpperLeg, "LeftHip", "BallSocket")
ragdollJoint(character.LowerTorso, character.RightUpperLeg, "RightHip", "BallSocket")
