Ragdoll a player on a Touch Event

Hello! I am seeking some help on making a player ragdoll when they touch a brick. I am using the Roblox Ragdoll Module script.

I’ve tried doing it myself, but when I touch the brick, the player’s camera goes into free mode and the player is just stuck there with body parts hanging through the ground which is something I don’t want.

Here is a clip for reference.

** Here is the code!**

local Part = script.Parent
local Ragdoll = require(game.ServerScriptService:WaitForChild("Ragdoll")) -- Roblox Ragdoll Module 

local Debounce = true

Part.Touched:Connect(function(hit)
	local Hum = hit.Parent:FindFirstChild("Humanoid")
	local Char = Hum.Parent
	if Hum and Debounce == true then
		print("HELLO")
		Debounce = false
		local function OnDeath()
			Ragdoll(Char, Hum)
			Char.Head.CanCollide = true
		end
		OnDeath()
		wait(4)
		Debounce = true
	end
end)

I’ve looked for solutions on Devforum but can’t find any that were relevant to my problem. I’ve tried making a death ragdoll and it works perfectly. I used pretty much the same ragdoll function but in the character health script. For some reason, I can’t replicate this to the touch event.

CODE:

local Character = script.Parent
local Humanoid = Character:WaitForChild'Humanoid'

Humanoid.BreakJointsOnDeath = false
local Ragdoll = require(game.ServerScriptService:WaitForChild("Ragdoll"))

local function OnDeath()
	Ragdoll(Character, Humanoid)
	Character.Head.CanCollide = true
end

Humanoid.Died:Connect(OnDeath)

Thank you!

2 Likes

You have two sets of code in the topic.
Would you please edit it and remove one so we are not confused as we are now.
Can you put some more prints in and show us the flow of the logic.

When you print “HELLO” does it only print once. Does breakjointsondeath affect the ragdoll? Maybe you forgot to set it up to false on hit function.

Yes, it only prints “hello” once. Also, the Ragdoll function runs but it makes the character glitchy like in the video.

Does the Humanoid.BreakJointsOnDeath affects the ragdoll? Try setting it to false on hit function.

You are not providing the ragdoll code which I believe would be the root of the issue.

The Humanoid.BreakJointsOnDeath works perfectly when the character dies, but my problem is that I don’t want to constantly kill the character to get the ragdoll effect. I am looking for a way to trigger the ragdoll without killing the character.

Btw, thanks for willing to help and reply quickly!

1 Like

Here is the module roblox provided for ragdoll.

local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")

local RigTypes = require(script.RigTypes)

local RAGDOLLED_TAG = "__Ragdoll_Active"

local function ragdoll(model, humanoid)
	assert(humanoid:IsDescendantOf(model))
	if CollectionService:HasTag(model, RAGDOLLED_TAG) then
		return
	end
	CollectionService:AddTag(model, RAGDOLLED_TAG)

	-- Turn into loose body:
	humanoid:ChangeState(Enum.HumanoidStateType.Physics)

	-- Instantiate BallSocketConstraints:
	local attachments = RigTypes.getAttachments(model, humanoid.RigType)
	for name, objects in pairs(attachments) do
		local parent = model:FindFirstChild(name)
		if parent then
			local constraint = Instance.new("BallSocketConstraint")
			constraint.Name = "RagdollBallSocketConstraint"
			constraint.Attachment0 = objects.attachment0
			constraint.Attachment1 = objects.attachment1
			constraint.LimitsEnabled = true
			constraint.UpperAngle = objects.limits.UpperAngle
			constraint.TwistLimitsEnabled = true
			constraint.TwistLowerAngle = objects.limits.TwistLowerAngle
			constraint.TwistUpperAngle = objects.limits.TwistUpperAngle
			constraint.Parent = parent
		end
	end

	-- Instantiate NoCollisionConstraints:
	local parts = RigTypes.getNoCollisions(model, humanoid.RigType)
	for _, objects in pairs(parts) do
		local constraint = Instance.new("NoCollisionConstraint")
		constraint.Name = "RagdollNoCollisionConstraint"
		constraint.Part0 = objects[1]
		constraint.Part1 = objects[2]
		constraint.Parent = objects[1]
	end

	-- Destroy all regular joints:
	for _, motor in pairs(model:GetDescendants()) do
		if motor:IsA("Motor6D") then
			motor:Destroy()
		end
	end
end

return ragdoll

Under that Module is another module script.

local RigTypes = {}

local HEAD_LIMITS = {
	UpperAngle = 60;
	TwistLowerAngle = -60;
	TwistUpperAngle = 60;
}

local LOWER_TORSO_LIMITS = {
	UpperAngle = 20;
	TwistLowerAngle = -30;
	TwistUpperAngle = 60;
}

local HAND_FOOT_LIMITS = {
	UpperAngle = 10;
	TwistLowerAngle = -10;
	TwistUpperAngle = 10;
}

local ELBOW_LIMITS = {
	UpperAngle = 30;
	TwistLowerAngle = 0;
	TwistUpperAngle = 120;
}

local KNEE_LIMITS = {
	UpperAngle = 30;
	TwistLowerAngle = -120;
	TwistUpperAngle = 0;
}

local SHOULDER_LIMITS = {
	UpperAngle = 60;
	TwistLowerAngle = -60;
	TwistUpperAngle = 175;
}

local HIP_LIMITS = {
	UpperAngle = 40;
	TwistLowerAngle = -5;
	TwistUpperAngle = 150;
}

local R6_HEAD_LIMITS = {
	UpperAngle = 30;
	TwistLowerAngle = -60;
	TwistUpperAngle = 60;
}

local R6_SHOULDER_LIMITS = {
	UpperAngle = 90;
	TwistLowerAngle = -30;
	TwistUpperAngle = 175;
}

local R6_HIP_LIMITS = {
	UpperAngle = 60;
	TwistLowerAngle = -5;
	TwistUpperAngle = 120;
}

local function createJointData(attach0, attach1, limits)
	assert(attach0)
	assert(attach1)
	assert(limits)
	assert(limits.UpperAngle >= 0)
	assert(limits.TwistLowerAngle <= limits.TwistUpperAngle)

	return {
		attachment0 = attach0,
		attachment1 = attach1,
		limits = limits
	}
end

local function find(model)
	return function(first, second, limits)
		local part0 = model:FindFirstChild(first[1])
		local part1 = model:FindFirstChild(second[1])
		if part0 and part1 then
			local attach0 = part0:FindFirstChild(first[2])
			local attach1 = part1:FindFirstChild(second[2])
			if attach0 and attach1 and attach0:IsA("Attachment") and attach1:IsA("Attachment") then
				return createJointData(attach0, attach1, limits)
			end
		end
	end
end

function RigTypes.getNoCollisions(model, rigType)
	if rigType == Enum.HumanoidRigType.R6 then
		return RigTypes.getR6NoCollisions(model)
	elseif rigType == Enum.HumanoidRigType.R15 then
		return RigTypes.getR15NoCollisions(model)
	else
		return {}
	end
end

-- Get list of attachments to make ballsocketconstraints between:
function RigTypes.getAttachments(model, rigType)
	if rigType == Enum.HumanoidRigType.R6 then
		return RigTypes.getR6Attachments(model)
	elseif rigType == Enum.HumanoidRigType.R15 then
		return RigTypes.getR15Attachments(model)
	else
		return {}
	end
end

function RigTypes.getR6Attachments(model)
	local rightLegAttachment = Instance.new("Attachment")
	rightLegAttachment.Name = "RagdollRightLegAttachment"
	rightLegAttachment.Position = Vector3.new(0, 1, 0)
	rightLegAttachment.Parent = model:FindFirstChild("Right Leg")

	local leftLegAttachment = Instance.new("Attachment")
	leftLegAttachment.Name = "RagdollLeftLegAttachment"
	leftLegAttachment.Position = Vector3.new(0, 1, 0)
	leftLegAttachment.Parent = model:FindFirstChild("Left Leg")

	local torsoLeftAttachment = Instance.new("Attachment")
	torsoLeftAttachment.Name = "RagdollTorsoLeftAttachment"
	torsoLeftAttachment.Position = Vector3.new(-0.5, -1, 0)
	torsoLeftAttachment.Parent = model:FindFirstChild("Torso")

	local torsoRightAttachment = Instance.new("Attachment")
	torsoRightAttachment.Name = "RagdollTorsoRightAttachment"
	torsoRightAttachment.Position = Vector3.new(0.5, -1, 0)
	torsoRightAttachment.Parent = model:FindFirstChild("Torso")

	local headAttachment = Instance.new("Attachment")
	headAttachment.Name = "RagdollHeadAttachment"
	headAttachment.Position = Vector3.new(0, -0.5, 0)
	headAttachment.Parent = model:FindFirstChild("Head")

	local leftArmAttachment = Instance.new("Attachment")
	leftArmAttachment.Name = "RagdollLeftArmAttachment"
	leftArmAttachment.Position = Vector3.new(0.5, 1, 0)
	leftArmAttachment.Parent = model:FindFirstChild("Left Arm")

	local ragdollRightArmAttachment = Instance.new("Attachment")
	ragdollRightArmAttachment.Name = "RagdollRightArmAttachment"
	ragdollRightArmAttachment.Position = Vector3.new(-0.5, 1, 0)
	ragdollRightArmAttachment.Parent = model:FindFirstChild("Right Arm")

	local query = find(model)

	return {
		Head = query(
			{"Torso", "NeckAttachment"},
			{"Head", "RagdollHeadAttachment"},
			R6_HEAD_LIMITS),
		["Left Arm"] = query(
			{"Torso", "LeftCollarAttachment"},
			{"Left Arm", "RagdollLeftArmAttachment"},
			R6_SHOULDER_LIMITS),
		["Right Arm"] = query(
			{"Torso", "RightCollarAttachment"},
			{"Right Arm", "RagdollRightArmAttachment"},
			R6_SHOULDER_LIMITS),
		["Left Leg"] = createJointData(torsoLeftAttachment, leftLegAttachment, R6_HIP_LIMITS),
		["Right Leg"] = createJointData(torsoRightAttachment, rightLegAttachment, R6_HIP_LIMITS),
	}
end

function RigTypes.getR15Attachments(model)
	local query = find(model)

	return {
		Head = query(
			{"UpperTorso", "NeckRigAttachment"},
			{"Head", "NeckRigAttachment"},
			HEAD_LIMITS),

		LowerTorso = query(
			{"UpperTorso", "WaistRigAttachment"},
			{"LowerTorso", "RootRigAttachment"},
			LOWER_TORSO_LIMITS),

		LeftUpperArm = query(
			{"UpperTorso", "LeftShoulderRigAttachment"},
			{"LeftUpperArm", "LeftShoulderRigAttachment"},
			SHOULDER_LIMITS),
		LeftLowerArm = query(
			{"LeftUpperArm", "LeftElbowRigAttachment"},
			{"LeftLowerArm", "LeftElbowRigAttachment"},
			ELBOW_LIMITS),
		LeftHand = query(
			{"LeftLowerArm", "LeftWristRigAttachment"},
			{"LeftHand", "LeftWristRigAttachment"},
			HAND_FOOT_LIMITS),

		RightUpperArm = query(
			{"UpperTorso", "RightShoulderRigAttachment"},
			{"RightUpperArm", "RightShoulderRigAttachment"},
			SHOULDER_LIMITS),
		RightLowerArm = query(
			{"RightUpperArm", "RightElbowRigAttachment"},
			{"RightLowerArm", "RightElbowRigAttachment"},
			ELBOW_LIMITS),
		RightHand = query(
			{"RightLowerArm", "RightWristRigAttachment"},
			{"RightHand", "RightWristRigAttachment"},
			HAND_FOOT_LIMITS),

		LeftUpperLeg = query(
			{"LowerTorso", "LeftHipRigAttachment"},
			{"LeftUpperLeg", "LeftHipRigAttachment"},
			HIP_LIMITS),
		LeftLowerLeg = query(
			{"LeftUpperLeg", "LeftKneeRigAttachment"},
			{"LeftLowerLeg", "LeftKneeRigAttachment"},
			KNEE_LIMITS),
		LeftFoot = query(
			{"LeftLowerLeg", "LeftAnkleRigAttachment"},
			{"LeftFoot", "LeftAnkleRigAttachment"},
			HAND_FOOT_LIMITS),

		RightUpperLeg = query(
			{"LowerTorso", "RightHipRigAttachment"},
			{"RightUpperLeg", "RightHipRigAttachment"},
			HIP_LIMITS),
		RightLowerLeg = query(
			{"RightUpperLeg", "RightKneeRigAttachment"},
			{"RightLowerLeg", "RightKneeRigAttachment"},
			KNEE_LIMITS),
		RightFoot = query(
			{"RightLowerLeg", "RightAnkleRigAttachment"},
			{"RightFoot", "RightAnkleRigAttachment"},
			HAND_FOOT_LIMITS),
	}
end

function RigTypes.getR6NoCollisions(model)
	local list = {}

	local function addPair(pair)
		local part0 = model:FindFirstChild(pair[1])
		local part1 = model:FindFirstChild(pair[2])

		if part0 and part1 then
			table.insert(list, {part0, part1})
		end
	end

	addPair({"Head", "Torso"})
	addPair({"Left Arm", "Torso"})
	addPair({"Right Arm", "Torso"})
	addPair({"Left Leg", "Torso"})
	addPair({"Right Leg", "Torso"})

	addPair({"Left Leg", "Right Leg"})

	return list
end


function RigTypes.getR15NoCollisions(model)
	local list = {}

	local function addPair(pair)
		local part0 = model:FindFirstChild(pair[1])
		local part1 = model:FindFirstChild(pair[2])

		if part0 and part1 then
			table.insert(list, {part0, part1})
		end
	end

	addPair({"Head", "UpperTorso"})
	addPair({"UpperTorso", "LowerTorso"})

	addPair({"UpperTorso", "LeftUpperArm"})
	addPair({"LowerTorso", "LeftUpperArm"})
	addPair({"LeftUpperArm", "LeftLowerArm"})
	addPair({"LeftLowerArm", "LeftHand"})
	addPair({"LeftUpperArm", "LeftHand"})

	addPair({"UpperTorso", "RightUpperArm"})
	addPair({"LowerTorso", "RightUpperArm"})
	addPair({"RightUpperArm", "RightLowerArm"})
	addPair({"RightLowerArm", "RightHand"})
	addPair({"RightUpperArm", "RightHand"})

	addPair({"LeftUpperLeg", "RightUpperLeg"})

	addPair({"UpperTorso", "RightUpperLeg"})
	addPair({"LowerTorso", "RightUpperLeg"})
	addPair({"RightUpperLeg", "RightLowerLeg"})
	addPair({"RightLowerLeg", "RightFoot"})
	addPair({"RightUpperLeg", "RightFoot"})

	addPair({"UpperTorso", "LeftUpperLeg"})
	addPair({"LowerTorso", "LeftUpperLeg"})
	addPair({"LeftUpperLeg", "LeftLowerLeg"})
	addPair({"LeftLowerLeg", "LeftFoot"})
	addPair({"LeftUpperLeg", "LeftFoot"})

	-- Support weird R15 rigs
	addPair({"UpperTorso", "LeftLowerLeg"})
	addPair({"UpperTorso", "RightLowerLeg"})
	addPair({"LowerTorso", "LeftLowerLeg"})
	addPair({"LowerTorso", "RightLowerLeg"})

	addPair({"UpperTorso", "LeftLowerArm"})
	addPair({"UpperTorso", "RightLowerArm"})

	local upperTorso = model:FindFirstChild("UpperTorso")
	if upperTorso and upperTorso.Size.x <= 1.5 then
		addPair({"Head", "LeftUpperArm"})
		addPair({"Head", "RightUpperArm"})
	end

	return list
end

return RigTypes
1 Like