Ragdoll script killing player randomly

I am making a ragdoll script for my game, however, I have encountered a pretty massive problem.
The script sometimes kills the player when it is supposed to end, so I know that the issue is in the last lines after wait(Time)

I feel like it has something to do with the Neck property, however I would like help with finding out the lines that are problematic, and how to fix them.

Please note that this script may not work if you try using it in an alternate studio.

function Ragdoll(Hum, Time)
	local c = Hum.Parent
	c.Humanoid.BreakJointsOnDeath = false
	c.PlayerStuff.Values.Ragdolled.Value = true
	Hum.PlatformStand = true
	Hum.AutoRotate = false
	Hum.JumpHeight = 0
	Hum.Parent.PlayerStuff.Events.HideInv:FireAllClients()
	Hum:UnequipTools()
	Hum:ChangeState(Enum.HumanoidStateType.GettingUp)
	for _, v in pairs(c:GetDescendants()) do
		if v:IsA("Motor6D") and v.Name ~= "Root Hip" and v.Name ~= "RootJoint" and v.Name ~= "Handle" then
			local a0, a1 = Instance.new("Attachment"), Instance.new("Attachment")
			a0.CFrame = v.C0
			a1.CFrame = v.C1
			a0.Parent = v.Part0
			a1.Parent = v.Part1

			local b = Instance.new("BallSocketConstraint")
			b.LimitsEnabled = true
			b.Radius = 10
			b.Attachment0 = a0
			b.Attachment1 = a1
			b.Parent = v.Part1

			if b then
				if string.match(b.Parent.Name, "Arm") then
					b.UpperAngle = 90
					b.TwistLowerAngle = -90
					b.TwistUpperAngle = 90
					b.Attachment0.Axis = Vector3.new(1,0,0)
					b.Attachment0.SecondaryAxis = Vector3.new(0,1,0)
					b.Attachment1.Axis = Vector3.new(1,0,0)
					b.Attachment1.SecondaryAxis = Vector3.new(0,1,0)
				elseif string.match(b.Parent.Name, "Leg") then
					b.UpperAngle = 35
					b.TwistLowerAngle = -90
					b.TwistUpperAngle = 90
					b.Attachment0.Axis = Vector3.new(1,0,0)
					b.Attachment0.SecondaryAxis = Vector3.new(0,0,0)
					b.Attachment1.Axis = Vector3.new(1,0,0)
					b.Attachment1.SecondaryAxis = Vector3.new(0,0,0)
				elseif string.match(b.Parent.Name, "Head") then
					b.UpperAngle = 55
					b.TwistLowerAngle = -75
					b.TwistUpperAngle = 45
					b.Attachment0.Axis = Vector3.new(1,0,0)
					b.Attachment0.SecondaryAxis = Vector3.new(0,1,0)
					b.Attachment1.Axis = Vector3.new(1,0,0)
					b.Attachment1.SecondaryAxis = Vector3.new(0,1,0)
				end
			end

			local Part = Instance.new("Part", v.Part1)
			Part.Size = v.Part1.Size - Vector3.new(0.25,0.5,0.25)
			Part.Name = "Collider"
			Part.CFrame = v.Part1.CFrame
			Part.Transparency = 1



			local Weld = Instance.new("Weld", Part)
			Weld.Part0 = Part
			Weld.Part1 = v.Part1

			v.Enabled = false
		end
	end
	local HumanoidRootPart = c:FindFirstChild("HumanoidRootPart")
	local Torso = c:FindFirstChild("Torso")
	local Head = c:FindFirstChild("Head")
	if HumanoidRootPart then
		HumanoidRootPart.CanCollide = false
	else
	end
	wait(Time)
	if Hum.Health > 0 and not c.PlayerStuff.Values.BleedingOut.Value == true then
		if Hum.Health > 0 and not c.PlayerStuff.Values.BleedingOut.Value == true then
			Hum.PlatformStand = false
		end
		Hum.AutoRotate = true
		Hum.JumpHeight = 7.159
		c.PlayerStuff.Values.Ragdolled.Value = false
		Hum.Parent.PlayerStuff.Events.ShowInv:FireAllClients()
		for _, v in pairs(c:GetDescendants()) do
			if v:IsA("Motor6D") then
				v.Enabled = true
			end
		end
		for _, v in pairs(c:GetDescendants()) do
			if v:IsA("BallSocketConstraint") then
				if v.Attachment0 then
					v.Attachment0:Destroy()
				else
				end
				if v.Attachment1 then
					v.Attachment1:Destroy()
				else
				end
				if v then
					v:Destroy()
				else
				end
			end
		end
		for _, v in pairs(c:GetDescendants()) do
			if v:IsA("BasePart") and v.Name == "Collider" then
				v:Destroy()
			end
		end
		local HumanoidRootPart = c:FindFirstChild("HumanoidRootPart")
		local Torso = c:FindFirstChild("Torso")
		local Head = c:FindFirstChild("Head")
		if HumanoidRootPart then
			HumanoidRootPart.CanCollide = true
		else
			return
		end
		if c.PlayerStuff.Values.Downed.Value == false then
			Hum.Parent:WaitForChild("Torso").CFrame = CFrame.new (Hum.Parent:WaitForChild("Torso").Position) * CFrame.Angles (0, math.rad (90), 0)
		end
	end
end
1 Like