Ragdoll system collisions

I’m making a Ragdoll module, everything here is working except that the parts clip through the ground.
image
I’ve tried to set cancollide on to the parts of the player, but it’s not working. Is someone able to help me out?

local Ragdoll = {}

function Ragdoll.Ragdoll(EnemyChar)
	for _,rgd in ipairs(EnemyChar:GetDescendants()) do
		if rgd:IsA("Motor6D") and rgd.Parent.Name ~= "HumanoidRootPart" then
			local Ballsocket = Instance.new("BallSocketConstraint")
			Ballsocket.Name = "RagdollSocket"
			local Attach1 = Instance.new("Attachment")
			local Attach2 = Instance.new("Attachment")
			Attach1.Parent = rgd.Part0
			Attach2.Parent = rgd.Part1
			Ballsocket.Parent = rgd.Parent
			Ballsocket.Attachment0 = Attach1
			Ballsocket.Attachment1 = Attach2
			Attach1.CFrame = rgd.C0
			Attach2.CFrame = rgd.C1
			Ballsocket.LimitsEnabled = true
			Ballsocket.TwistLimitsEnabled = true
			rgd.Parent.CanCollide = true
			rgd:Destroy()
		end
	end
	EnemyChar.Humanoid.RequiresNeck = false
	EnemyChar.Humanoid.Sit = true
	
	local Collide = {
		[EnemyChar.LeftFoot.CanCollide] = true,
		[EnemyChar.LeftHand.CanCollide] = true,
		[EnemyChar.LeftLowerArm.CanCollide] = true,
		[EnemyChar.LeftLowerLeg.CanCollide] = true,
		[EnemyChar.LeftUpperArm.CanCollide] = true,
		[EnemyChar.LeftUpperLeg.CanCollide] = true,
		[EnemyChar.LowerTorso.CanCollide] = true,
		[EnemyChar.RightFoot.CanCollide] = true,
		[EnemyChar.RightHand.CanCollide] = true,
		[EnemyChar.RightLowerArm.CanCollide] = true,
		[EnemyChar.RightLowerLeg.CanCollide] = true,
		[EnemyChar.RightUpperArm.CanCollide] = true,
		[EnemyChar.RightUpperLeg.CanCollide] = true,
		[EnemyChar.UpperTorso.CanCollide] = true,
		[EnemyChar.Head.CanCollide] = true
	}
end

function Ragdoll.UnRagdoll(EnemyChar)
	for _,urgd in ipairs(EnemyChar:GetDescendants()) do
		if urgd:IsA("BallSocketConstraint") then
			urgd.UpperAngle = 0
			urgd.TwistUpperAngle = 0
			urgd.TwistLowerAngle = 0
			local Joints = Instance.new("Motor6D", urgd.Parent)
			Joints.Part0 = urgd.Attachment0.Parent
			Joints.Part1 = urgd.Attachment1.Parent
			Joints.C0 = urgd.Attachment0.CFrame
			Joints.C1 = urgd.Attachment1.CFrame
			urgd.Parent.CanCollide = false
			urgd:Destroy()
		end
	end
	EnemyChar.Humanoid.Sit = false
	
	local UnCollide = {
		[EnemyChar.LeftFoot.CanCollide] = false,
		[EnemyChar.LeftHand.CanCollide] = false,
		[EnemyChar.LeftLowerArm.CanCollide] = false,
		[EnemyChar.LeftLowerLeg.CanCollide] = false,
		[EnemyChar.LeftUpperArm.CanCollide] = false,
		[EnemyChar.LeftUpperLeg.CanCollide] = false,
		[EnemyChar.LowerTorso.CanCollide] = false,
		[EnemyChar.RightFoot.CanCollide] = false,
		[EnemyChar.RightHand.CanCollide] = false,
		[EnemyChar.RightLowerArm.CanCollide] = false,
		[EnemyChar.RightLowerLeg.CanCollide] = false,
		[EnemyChar.RightUpperArm.CanCollide] = false,
		[EnemyChar.RightUpperLeg.CanCollide] = false,
		[EnemyChar.UpperTorso.CanCollide] = false,
		[EnemyChar.Head.CanCollide] = false
	}
end
return Ragdoll

Try fire to the client and set their humanoid state to physics

Set the humanoid state to Ragdoll

Humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)

This code creates a table, which you don’t use, so it essentially does nothing.

Instead, do this:

EnemyChar.LeftFoot.CanCollide = true
-- ... etc.

Note that not all the parts of the character have CanCollide off to begin with, so don’t set all of the parts to non-collide when resetting.

I’d also recommend using NoCollisionContraints between some of the joints:

https://developer.roblox.com/en-us/api-reference/class/NoCollisionConstraint

Didn’t work, the parts still phase into the ground

1 Like

Seems to be because EnemyChar.Humanoid.Sit = true is overwriting the state from Ragdoll to Sitting. Try removing it

EDIT: Forgot to mention that you should also disable GettingUp EnemyChar.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false) so that the ragdoll doesn’t “wake up” randomly

Oh, alright. Also how do you reset the Humanoids state back to default? For the unragdoll. And instead of disabling getting up, could I just enable platform stand?

1: Just re-enable GettingUp & do EnemyChar.Humanoid:ChangeState(Enum.HumanoidStateType.Running).
2: PlatformStand overwrites the Ragdoll state, so no.

It works fine for a moment, but then they just randomly start doing this.
https://gyazo.com/907d32e6d290e51bc0aaa4733bfdf5b5

Heres the updated code:

local Ragdoll = {}

function Ragdoll.Ragdoll(EnemyChar)
	for _,rgd in ipairs(EnemyChar:GetDescendants()) do
		if rgd:IsA("Motor6D") and rgd.Parent.Name ~= "HumanoidRootPart" then
			local Ballsocket = Instance.new("BallSocketConstraint")
			Ballsocket.Name = "RagdollSocket"
			local Attach1 = Instance.new("Attachment")
			local Attach2 = Instance.new("Attachment")
			Attach1.Parent = rgd.Part0
			Attach2.Parent = rgd.Part1
			Ballsocket.Parent = rgd.Parent
			Ballsocket.Attachment0 = Attach1
			Ballsocket.Attachment1 = Attach2
			Attach1.CFrame = rgd.C0
			Attach2.CFrame = rgd.C1
			Ballsocket.LimitsEnabled = true
			Ballsocket.TwistLimitsEnabled = true
			rgd.Parent.CanCollide = true
			EnemyChar.Humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
			rgd:Destroy()
		end
	end
	EnemyChar.Humanoid.RequiresNeck = false
	EnemyChar.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)

		EnemyChar.LeftFoot.CanCollide = true
		EnemyChar.LeftHand.CanCollide = true
		EnemyChar.LeftLowerArm.CanCollide = true
		EnemyChar.LeftLowerLeg.CanCollide = true
		EnemyChar.LeftUpperArm.CanCollide = true
		EnemyChar.LeftUpperLeg.CanCollide = true
		EnemyChar.LowerTorso.CanCollide = true
		EnemyChar.RightFoot.CanCollide = true
		EnemyChar.RightHand.CanCollide = true
		EnemyChar.RightLowerArm.CanCollide = true
		EnemyChar.RightLowerLeg.CanCollide = true
		EnemyChar.RightUpperArm.CanCollide = true
		EnemyChar.RightUpperLeg.CanCollide = true
		EnemyChar.UpperTorso.CanCollide = true
		EnemyChar.Head.CanCollide = true
end

function Ragdoll.UnRagdoll(EnemyChar)
	for _,urgd in ipairs(EnemyChar:GetDescendants()) do
		if urgd:IsA("BallSocketConstraint") then
			urgd.UpperAngle = 0
			urgd.TwistUpperAngle = 0
			urgd.TwistLowerAngle = 0
			local Joints = Instance.new("Motor6D", urgd.Parent)
			Joints.Part0 = urgd.Attachment0.Parent
			Joints.Part1 = urgd.Attachment1.Parent
			Joints.C0 = urgd.Attachment0.CFrame
			Joints.C1 = urgd.Attachment1.CFrame
			urgd.Parent.CanCollide = false
			urgd:Destroy()
		end
	end
	EnemyChar.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
	EnemyChar.Humanoid:ChangeState(Enum.HumanoidStateType.Running)

		EnemyChar.LeftFoot.CanCollide = false
		EnemyChar.LeftHand.CanCollide = false
		EnemyChar.LeftLowerArm.CanCollide = false
		EnemyChar.LeftLowerLeg.CanCollide = false
		EnemyChar.LeftUpperArm.CanCollide = false
		EnemyChar.LeftUpperLeg.CanCollide = false
		EnemyChar.LowerTorso.CanCollide = false
		EnemyChar.RightFoot.CanCollide = false
		EnemyChar.RightHand.CanCollide = false
		EnemyChar.RightLowerArm.CanCollide = false
		EnemyChar.RightLowerLeg.CanCollide = false
		EnemyChar.RightUpperArm.CanCollide = false
		EnemyChar.RightUpperLeg.CanCollide = false
		EnemyChar.UpperTorso.CanCollide = false
		EnemyChar.Head.CanCollide = false

end
return Ragdoll