Help with r6 ragdoll script

I want to make a ragdoll and I have achieved that except there is just one bug where the body parts will sometimes rotate too much to the point where they are facing backwards

Here is a clip and as you can see the leg is facing backwards


local character = script.Parent

character.Humanoid.AutoRotate = false
character.Humanoid.PlatformStand = true
character.Humanoid.WalkSpeed = 0

-- find out where to put the ball constraint attachments
for _, motor6D in pairs(character:GetDescendants()) do
	if motor6D:IsA("Motor6D") then

		local motor6DName = motor6D.Name
		local AxisAttachment = Instance.new("Attachment")

		if motor6DName == "Left Hip" then
			AxisAttachment.CFrame = motor6D.C0 * CFrame.new(0,0,-0.5)
		elseif motor6DName == "Right Hip"  then
			AxisAttachment.CFrame = motor6D.C0 * CFrame.new(0,0,-0.5)
		else
			AxisAttachment.CFrame = motor6D.C0
		end
		AxisAttachment.Name = motor6DName .. "RigAttachment"
		AxisAttachment.Parent = motor6D.Part0

		local JointAttachment = Instance.new("Attachment")
		if motor6DName == "Left Hip" then
			JointAttachment.CFrame = motor6D.C1 * CFrame.new(0,0,-0.5)
		elseif motor6DName == "Right Hip"  then
			JointAttachment.CFrame = motor6D.C1 * CFrame.new(0,0,-0.5)
		else
			JointAttachment.CFrame = motor6D.C1
		end
		JointAttachment.Name = motor6DName .. "RigAttachment2"
		JointAttachment.Parent = motor6D.Part1

		local ballConstraint = Instance.new("BallSocketConstraint")
		ballConstraint.Attachment0 = AxisAttachment
		ballConstraint.Attachment1 = JointAttachment
		ballConstraint.Parent = motor6D.Parent

		if motor6D.Name ~= "RootJoint" and motor6D.Name ~= "Neck" then
			motor6D.Enabled = false
		end
	end
end

-- make new body parts so the ragdolled ones dont just fall through the floor
for _, otherBodyPart in pairs(character:GetChildren()) do
	if otherBodyPart:IsA("BasePart") then
		local bodyPart = otherBodyPart:Clone()
		for _, stuff in pairs(bodyPart:GetChildren()) do
			stuff:Destroy()
		end
		bodyPart.CanCollide = true
		bodyPart.Size = bodyPart.Size * Vector3.new(1,0.5,1)
		bodyPart.Name = "bodyClone"
		local weldConstraint = Instance.new("WeldConstraint")
		weldConstraint.Parent = bodyPart
		bodyPart.Parent = otherBodyPart
		weldConstraint.Part0 = otherBodyPart
		weldConstraint.Part1 = bodyPart
		bodyPart.Transparency = 1
		bodyPart.Name = otherBodyPart.Name.."BODYPART"
	end
end

I didn’t include the entire script because I felt like it wouldn’t help and it would just make it harder to read

1 Like

To fix this you can modify the ball socket constraint properties so you limit rotation, check these properties

1 Like