Unragdolling player makes them flip

I have made a script that ragdolls and unragdolls the player but there is a problem where every few times the player will flip when they are unragdolled

https://i.gyazo.com/482efbbed570e14dd64042f079a52f33.mp4

heres the script

function Ragdoll(Hum)
	local c = Hum.Parent
	c.Humanoid.BreakJointsOnDeath = false
	Hum.PlatformStand = true
	for _, v in pairs(c:GetDescendants()) do
		if v:IsA("Motor6D") and v.Name ~= "Neck" 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 string.match(b.Parent.Name, "Arm") then
				b.UpperAngle = 25
				b.TwistLowerAngle = -45
				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)
			elseif string.match(b.Parent.Name, "Leg") then
				b.UpperAngle = 35
				b.TwistLowerAngle = -7.5
				b.TwistUpperAngle = 15
				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, "Head") then
				b.UpperAngle = 45
				b.TwistLowerAngle = -45
				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

			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

			delay(0.2, function()
				local Sound = Instance.new("Sound", c:WaitForChild("Torso"))
				game.Debris:AddItem(Sound, 0.5)
				Sound.SoundId = "rbxassetid://3690089300"	
				Sound.PlaybackSpeed = 1
				Sound.Volume = 0.1
				Sound:Play()
			end)
		end
	end
	local HumanoidRootPart = c:FindFirstChild("HumanoidRootPart")
	if HumanoidRootPart then
		HumanoidRootPart.CanCollide = false
	else
		return
	end
end

function Unragdoll(Hum)
	local c = Hum.Parent
	Hum.PlatformStand = false
	c.Torso.Orientation = Vector3.new(0,0,0)
	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
			v:Destroy()
		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")
	if HumanoidRootPart then
		HumanoidRootPart.CanCollide = true
	else
		return
	end
end

4 Likes

rather than do all this script could you not jus kill the player but tp them back to their place in less than a second?

1 Like

You could do that but I don’t want them to lose all of their tools

2 Likes

You could make the tools save to the player or if everyone has the same tools just put it in starter gear.

1 Like

I had this same issue. You can fix it by calling Humanoid:ChangeState and changing the state to Enum.HumanoidStateType.GettingUp when you’re unragdolling.

3 Likes