Why does my ragdoll script do this?

function module:ApplyRagdoll(character: Model, duration: number)
	duration = duration or 3.5
	
	if character:GetAttribute("Blocking") == true then return end
	
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
	
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if not humanoid or not hrp then return end
	
	humanoid.AutoRotate = false
	humanoid.BreakJointsOnDeath = false
	humanoid.RequiresNeck = false
	
	local offsets = {
		Head = {
			CFrame = {CFrame.new(0, 1, 0, 0, -1, 0, 1, 0, -0, 0, 0, 1), CFrame.new(0, -0.5, 0, 0, -1, 0, 1, 0, -0, 0, 0, 1)}
		},
		HumanoidRootPart = {
			CFrame = {CFrame.new(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CFrame.new(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)}
		},
		["Right Arm"] = {
			CFrame = {CFrame.new(1.3, 0.75, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1), CFrame.new(-0.2, 0.75, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)}
		},
		["Left Arm"] = {
			CFrame = {CFrame.new(-1.3, 0.75, 0, -1, 0, 0, 0, -1, 0, 0, 0, 1), CFrame.new(0.2, 0.75, 0, -1, 0, 0, 0, -1, 0, 0, 0, 1)}
		},
		["Right Leg"] = {
			CFrame = {CFrame.new(0.5, -1, 0, 0, 1, -0, -1, 0, 0, 0, 0, 1), CFrame.new(0, 1, 0, 0, 1, -0, -1, 0, 0, 0, 0, 1)}
		},
		["Left Leg"] = {
			CFrame = {CFrame.new(-0.5, -1, 0, 0, 1, -0, -1, 0, 0, 0, 0, 1), CFrame.new(0, 1, 0, 0, 1, -0, -1, 0, 0, 0, 0, 1)}
		}
	}
	
	local function buildCollisionParts()
		for _, v in character:GetChildren() do
			if v:IsA("BasePart") and v ~= hrp then
				local p = v:Clone()
				p.Parent = v
				p.CanCollide = false
				p.Massless = true
				p.Size = Vector3.one
				p.Name = "Collide"
				p.Transparency = 1
				p:ClearAllChildren()
				
				local weld = Instance.new("Weld")
				weld.Parent = p
				weld.Part0 = v
				weld.Part1 = p
			end
		end
	end
	
	buildCollisionParts()
	
	local function enableMotor6D(enabled: boolean)
		for _, v in character:GetDescendants() do
			if v.Name == "Handle" or v.Name == "RootJoint" or v.Name == "Neck" then continue end
			if v:IsA("Motor6D") then v.Enabled = enabled end
		end
	end
	
	local function buildJoints()
		for _, v in character:GetDescendants() do
			if not v:IsA("BasePart") or v:FindFirstAncestorOfClass("Accessory") or v.Name == "Handle" or v.Name == "Torso" or v == hrp or not offsets[v.Name] then continue end
			
			local a0: Attachment, a1: Attachment = Instance.new("Attachment"), Instance.new("Attachment")
			local joint: Constraint = Instance.new("BallSocketConstraint")
			
			a0.Name = "RAGDOLL_ATTACHMENT"
			a0.Parent = v
			a0.CFrame = offsets[v.Name].CFrame[2]
			
			a1.Name = "RAGDOLL_ATTACHMENT"
			a1.Parent = hrp
			a1.CFrame = offsets[v.Name].CFrame[1]
			
			joint.Name = "RAGDOLL_CONSTRAINT"
			joint.Parent = v
			joint.Attachment0 = a0
			joint.Attachment1 = a1
			
			v.Massless = true
		end
	end
	
	local function enableCollisionParts(enabled: boolean)
		for _, v in character:GetChildren() do
			if v:IsA("BasePart") and v ~= hrp then
				v.CanCollide = not enabled
				v.Collide.CanCollide = enabled
			end
		end
	end
	
	local function destroyJoints()
		for _, v in character:GetDescendants() do
			if v.Name == "RAGDOLL_ATTACHMENT" or v.Name == "RAGDOLL_CONSTRAINT" then v:Destroy() end
			
			if not v:IsA("BasePart") or v:FindFirstAncestorOfClass("Accessory") or v.Name == "Torso" or v.Name == "Head" then continue end
		end
	end
	
	local function fling()
		local force = Instance.new("LinearVelocity")
		force.RelativeTo = "World"
		force.MaxForce = 9999
		force.Attachment0 = hrp.RootAttachment
		force.VectorVelocity = hrp.CFrame.LookVector + Vector3.new(math.random(-15, 15), 35, math.random(-30, 30))
		force.Parent = hrp
		
		game.Debris:AddItem(force, 0.1)
		humanoid.PlatformStand = true
	end
	
	fling()
	enableMotor6D(false)
	buildJoints()
	enableCollisionParts(true)
	
	task.delay(duration, function()
		if not humanoid then return end
		if humanoid:GetState() == Enum.HumanoidStateType.Dead then return end
		
		humanoid.AutoRotate = true
		humanoid.PlatformStand = false
		humanoid.BreakJointsOnDeath = true
		humanoid.RequiresNeck = true
		
		enableMotor6D(true)
		enableCollisionParts(false)
	end)
end

The player gets up, but he’s still stuck. Any ideas why this happens?

1 Like

Maybe set the NPC’s HumanoidRootPart NetworkOwnership to “nil” at the start of the ragdoll?

Tried and it didn’t fix the problem.

After disabling the ragdoll, set the humanoid state to: Enum.HumanoidStateType.GettingUp

Make sure you’re doing it through a local script, I had this same issue not too long ago and this fixed it for me.

2 Likes

Well, that fixed it, but the issue now is that it only works after the first ragdoll. And even after the first ragdoll, sometimes it doesn’t work smoothly.

Are you making sure to change the humanoid state through a local script as well as disabling platformstand before changing the humanoid state to getting up?

I was doing it all from the same script as the ragdoll, which is a server script, and yes I did disable platformstand before changing the state.

Ah, that may be the reason for the odd behavior, for some reason changing the humanoid state through a server script also gave me some issues. (It not working at all) I’d recommend having a dedicated local script for this so that the server script fires the remote event and the local script listens for and receives that and sets the humanoid state to “GettingUp” Hopefully this fixes the issue you’ve been having!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.