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 :confused:

-- \\ Services // --
local Debris = game:GetService("Debris")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- \\ Main Variables // --
local Character = script.Parent
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")

local Activated = script:WaitForChild("Activated")
local StunDuration = script["StunDuration"].Value

local Thread = require(ReplicatedStorage:WaitForChild("Modules")["Thread"])

-- \\ Configuration // --
local LivingRagdoll = false

local EnableKnockback = true
local KnockbackOnToggleRagdoll = true
local Knockback = 3

-- \\ Debounces // --
local Ragdolled = false
local RagdolledPerm = false
local Stunned = false

-- \\ Functions // --
function IsLimbDecapitated(Limb)
	if Character:FindFirstChild("LimbDecapitation") and Character["LimbDecapitation"]:FindFirstChild(Limb.Name) and Character["LimbDecapitation"][Limb.Name]["Decapitated"].Value or not Character:FindFirstChild(Limb.Name) then
		return true
	else
		return false
	end
end

-- \\ Events // --
Humanoid.BreakJointsOnDeath = false

local function Ragdoll()
	local Object = Character:GetDescendants()
	for i = 1, #Object do
		if Object[i]:IsA("Tool") then
			Humanoid:UnequipTools()
		end
	end
	if Character:FindFirstChild("HumanoidRootPart") then
		Character.HumanoidRootPart.Massless = true
	end
	local Object = Character:GetDescendants()
	for i = 1, #Object do
		if Object[i]:IsA("Accessory") then
			Object[i].Handle.Massless = true
		end
	end
	local HumanoidRootPartAttachment = Instance.new("Attachment")
	local HeadAttachment = Instance.new("Attachment")
	local LeftArmAttachment = Instance.new("Attachment")
	local LeftLegAttachment = Instance.new("Attachment")
	local RightArmAttachment = Instance.new("Attachment")
	local RightLegAttachment = Instance.new("Attachment")
	local TorsoAttachment = Instance.new("Attachment")
	local TorsoAttachment1 = Instance.new("Attachment")
	local TorsoAttachment2 = Instance.new("Attachment")
	local TorsoAttachment3 = Instance.new("Attachment")
	local TorsoAttachment4 = Instance.new("Attachment")
	local TorsoAttachment5 = Instance.new("Attachment")

	local Head = Character:FindFirstChild("Head")
	local LeftArm = Character:FindFirstChild("Left Arm")
	local LeftLeg = Character:FindFirstChild("Left Leg")
	local RightArm = Character:FindFirstChild("Right Arm")
	local RightLeg = Character:FindFirstChild("Right Leg")
	local Torso = Character:FindFirstChild("Torso")
	if Head and not Head:FindFirstChild("HeadAttachment") and not IsLimbDecapitated(Head) then
		HeadAttachment.Name = "HeadAttachment"
		HeadAttachment.Parent = Head
		HeadAttachment.Position = Vector3.new(0, -0.5, 0)
		HeadAttachment.Rotation = Vector3.new(0, 0, 0)
		HeadAttachment.Axis = Vector3.new(1, 0, 0)
		HeadAttachment.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if LeftArm and not LeftArm:FindFirstChild("LeftArmAttachment") and not IsLimbDecapitated(LeftArm) then
		LeftArmAttachment.Name = "LeftArmAttachment"
		LeftArmAttachment.Parent = LeftArm
		LeftArmAttachment.Position = Vector3.new(0, 0.5, 0)
		LeftArmAttachment.Rotation = Vector3.new(0, 0, 0)
		LeftArmAttachment.Axis = Vector3.new(1, 0, 0)
		LeftArmAttachment.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if LeftLeg and not LeftLeg:FindFirstChild("LeftLegAttachment") and not IsLimbDecapitated(LeftLeg) then
		LeftLegAttachment.Name = "LeftLegAttachment"
		LeftLegAttachment.Parent = LeftLeg
		LeftLegAttachment.Position = Vector3.new(0, 1, 0)
		LeftLegAttachment.Rotation = Vector3.new(0, 0, 0)
		LeftLegAttachment.Axis = Vector3.new(1, 0, 0)
		LeftLegAttachment.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if RightArm and not RightArm:FindFirstChild("RightArmAttachment") and not IsLimbDecapitated(RightArm) then
		RightArmAttachment.Name = "RightArmAttachment"
		RightArmAttachment.Parent = RightArm
		RightArmAttachment.Position = Vector3.new(0, 0.5, 0)
		RightArmAttachment.Rotation = Vector3.new(0, 0, 0)
		RightArmAttachment.Axis = Vector3.new(1, 0, 0)
		RightArmAttachment.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if RightLeg and not RightLeg:FindFirstChild("RightLegAttachment") and not IsLimbDecapitated(RightLeg) then
		RightLegAttachment.Name = "RightLegAttachment"
		RightLegAttachment.Parent = RightLeg
		RightLegAttachment.Position = Vector3.new(0, 1, 0)
		RightLegAttachment.Rotation = Vector3.new(0, 0, 0)
		RightLegAttachment.Axis = Vector3.new(1, 0, 0)
		RightLegAttachment.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if Torso and not Torso:FindFirstChild("TorsoAttachment") and not IsLimbDecapitated(Torso) then
		TorsoAttachment.Name = "TorsoAttachment"
		TorsoAttachment.Parent = Torso
		TorsoAttachment.Position = Vector3.new(0.5, -1, 0)
		TorsoAttachment.Rotation = Vector3.new(0, 0, 0)
		TorsoAttachment.Axis = Vector3.new(1, 0, 0)
		TorsoAttachment.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if Torso and not Torso:FindFirstChild("TorsoAttachment1") and not IsLimbDecapitated(Torso) then
		TorsoAttachment1.Name = "TorsoAttachment1"
		TorsoAttachment1.Parent = Torso
		TorsoAttachment1.Position = Vector3.new(-0.5, -1, 0)
		TorsoAttachment1.Rotation = Vector3.new(0, 0, 0)
		TorsoAttachment1.Axis = Vector3.new(1, 0, 0)
		TorsoAttachment1.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if Torso and not Torso:FindFirstChild("TorsoAttachment2") and not IsLimbDecapitated(Torso) then
		TorsoAttachment2.Name = "TorsoAttachment2"
		TorsoAttachment2.Parent = Torso
		TorsoAttachment2.Position = Vector3.new(-1.5, 0.5, 0)
		TorsoAttachment2.Rotation = Vector3.new(0, 0, 0)
		TorsoAttachment2.Axis = Vector3.new(1, 0, 0)
		TorsoAttachment2.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if Torso and not Torso:FindFirstChild("TorsoAttachment3") and not IsLimbDecapitated(Torso) then
		TorsoAttachment3.Name = "TorsoAttachment3"
		TorsoAttachment3.Parent = Torso
		TorsoAttachment3.Position = Vector3.new(1.5, 0.5, 0)
		TorsoAttachment3.Rotation = Vector3.new(0, 0, 0)
		TorsoAttachment3.Axis = Vector3.new(1, 0, 0)
		TorsoAttachment3.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if Torso and not Torso:FindFirstChild("TorsoAttachment4") and not IsLimbDecapitated(Torso) then
		TorsoAttachment4.Name = "TorsoAttachment4"
		TorsoAttachment4.Parent = Torso
		TorsoAttachment4.Position = Vector3.new(0, 1, 0)
		TorsoAttachment4.Rotation = Vector3.new(0, 0, 0)
		TorsoAttachment4.Axis = Vector3.new(1, 0, 0)
		TorsoAttachment4.SecondaryAxis = Vector3.new(0, 1, 0)
	end
	if Torso and not Torso:FindFirstChild("TorsoAttachment5") and not IsLimbDecapitated(Torso) then
		TorsoAttachment5.Name = "TorsoAttachment5"
		TorsoAttachment5.Parent = Torso
		TorsoAttachment5.Position = Vector3.new(0, 0, 0)
		TorsoAttachment5.Rotation = Vector3.new(0, 90, 0)
		TorsoAttachment5.Axis = Vector3.new(0, 0, -1)
		TorsoAttachment5.SecondaryAxis = Vector3.new(0, 1, 0)
	end

	local Head = Character:FindFirstChild("Head")
	local LeftArm = Character:FindFirstChild("Left Arm")
	local LeftLeg = Character:FindFirstChild("Left Leg")
	local RightArm = Character:FindFirstChild("Right Arm")
	local RightLeg = Character:FindFirstChild("Right Leg")
	local Torso = Character:FindFirstChild("Torso")
	if LeftArm and not LeftArm:FindFirstChild("LeftArmBall") and not IsLimbDecapitated(LeftArm) then
		local LeftArmBallSocket = Instance.new("BallSocketConstraint")
		LeftArmBallSocket.Parent = LeftArm
		LeftArmBallSocket.Name = "LeftArmBall"
		LeftArmBallSocket.Attachment0 = LeftArmAttachment
		LeftArmBallSocket.Attachment1 = TorsoAttachment2
		LeftArmBallSocket.Enabled = true
		LeftArmBallSocket.LimitsEnabled = true
		LeftArmBallSocket.UpperAngle = 90
	end
	if RightArm and not RightArm:FindFirstChild("RightArmBall") and not IsLimbDecapitated(RightArm) then
		local RightArmBallSocket = Instance.new("BallSocketConstraint")
		RightArmBallSocket.Parent = RightArm
		RightArmBallSocket.Name = "RightArmBall"
		RightArmBallSocket.Attachment0 = RightArmAttachment
		RightArmBallSocket.Attachment1 = TorsoAttachment3
		RightArmBallSocket.Enabled = true
		RightArmBallSocket.LimitsEnabled = true
		RightArmBallSocket.UpperAngle = 90
	end
	if Head and not Head:FindFirstChild("HeadBall") and not IsLimbDecapitated(Head) then
		local HeadBallSocket = Instance.new("BallSocketConstraint")
		HeadBallSocket.Parent = Head
		HeadBallSocket.Name = "HeadBall"
		HeadBallSocket.Attachment0 = HeadAttachment
		HeadBallSocket.Attachment1 = TorsoAttachment4
		HeadBallSocket.Enabled = true
	end
	if Torso and not Torso:FindFirstChild("LeftLegBall") and not IsLimbDecapitated(Torso) then
		local LeftLegBallSocket = Instance.new("BallSocketConstraint")
		LeftLegBallSocket.Parent = Torso
		LeftLegBallSocket.Attachment0 = TorsoAttachment1
		LeftLegBallSocket.Name = "LeftLegBall"
		LeftLegBallSocket.Attachment1 = LeftLegAttachment
		LeftLegBallSocket.Enabled = true
		LeftLegBallSocket.LimitsEnabled = true
		LeftLegBallSocket.UpperAngle = 90
	end
	if Torso and not Torso:FindFirstChild("RightLegBall") and not IsLimbDecapitated(Torso) then
		local RightLegBallSocket = Instance.new("BallSocketConstraint")
		RightLegBallSocket.Parent = Torso
		RightLegBallSocket.Attachment0 = TorsoAttachment
		RightLegBallSocket.Attachment1 = RightLegAttachment
		RightLegBallSocket.Name = "RightLegBall"
		RightLegBallSocket.Enabled = true
		RightLegBallSocket.LimitsEnabled = true
		RightLegBallSocket.UpperAngle = 90
	end
	if LeftArm and not LeftArm:FindFirstChild("Joint") and not IsLimbDecapitated(LeftArm) then
		local Limb1 = Instance.new("Part")
		Limb1.Massless = true
		Limb1.Transparency = 1
		Limb1.Parent = LeftArm
		Limb1.Size = Vector3.new(1, 1, 1)
		Limb1.Name = "Joint"
		Limb1.Shape = "Ball"
		Limb1.CFrame = LeftArm.CFrame * CFrame.new(0, -0.5, 0)
		local Weld = Instance.new("Weld")
		Weld.Name = "RagdollWeld"
		Weld.Part0 = LeftArm
		Weld.Part1 = Limb1
		Weld.C0 = CFrame.new(0, -0.5, 0)
		Weld.Parent = LeftArm
	end
	if RightArm and not RightArm:FindFirstChild("Joint") and not IsLimbDecapitated(RightArm) then
		local Limb2 = Instance.new("Part")
		Limb2.Massless = true
		Limb2.Transparency = 1
		Limb2.Parent = RightArm
		Limb2.Size = Vector3.new(1, 1, 1)
		Limb2.Name = "Joint"
		Limb2.Shape = "Ball"
		Limb2.CFrame = RightArm.CFrame * CFrame.new(0, -0.5, 0)
		local Weld = Instance.new("Weld")
		Weld.Name = "RagdollWeld"
		Weld.Part0 = RightArm
		Weld.Part1 = Limb2
		Weld.C0 = CFrame.new(0, -0.5, 0)
		Weld.Parent = RightArm
	end
	if LeftLeg and not LeftLeg:FindFirstChild("Joint") and not IsLimbDecapitated(LeftLeg) then
		local Limb3 = Instance.new("Part")
		Limb3.Massless = true
		Limb3.Transparency = 1
		Limb3.Parent = LeftLeg
		Limb3.Size = Vector3.new(1, 1, 1)
		Limb3.Name = "Joint"
		Limb3.Shape = "Ball"
		Limb3.CFrame = LeftLeg.CFrame * CFrame.new(0, -0.5, 0)
		local Weld = Instance.new("Weld")
		Weld.Name = "RagdollWeld"
		Weld.Part0 = LeftLeg
		Weld.Part1 = Limb3
		Weld.C0 = CFrame.new(0, -0.5, 0)
		Weld.Parent = LeftLeg
	end
	if RightLeg and not RightLeg:FindFirstChild("Joint") and not IsLimbDecapitated(RightLeg) then
		local Limb4 = Instance.new("Part")
		Limb4.Massless = true
		Limb4.Transparency = 1
		Limb4.Parent = RightLeg
		Limb4.Size = Vector3.new(1, 1, 1)
		Limb4.Name = "Joint"
		Limb4.Shape = "Ball"
		Limb4.CFrame = RightLeg.CFrame * CFrame.new(0, -0.5, 0)
		local Weld = Instance.new("Weld")
		Weld.Name = "RagdollWeld"
		Weld.Part0 = RightLeg
		Weld.Part1 = Limb4
		Weld.C0 = CFrame.new(0, -0.5, 0)
		Weld.Parent = RightLeg
	end
	if Head and not Head:FindFirstChild("Joint") and not IsLimbDecapitated(Head) then
		local Limb5 = Instance.new("Part")
		Limb5.Massless = true
		Limb5.Transparency = 1
		Limb5.Parent = Head
		Limb5.Size = Vector3.new(0.8, 0.8, 0.8)
		Limb5.Name = "Joint"
		Limb5.Shape = "Ball"
		Limb5.CFrame = Head.CFrame * CFrame.new(0, 0.2, 0)
		local Weld = Instance.new("Weld")
		Weld.Name = "RagdollWeld"
		Weld.Part0 = Head
		Weld.Part1 = Limb5
		Weld.C0 = CFrame.new(0, 0, 0)
		Weld.Parent = Head
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Left Hip") then
		Character.Torso["Left Hip"].Part1 = nil
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Right Hip") then
		Character.Torso["Right Hip"].Part1 = nil
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Left Shoulder") then
		Character.Torso["Left Shoulder"].Part1 = nil
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Right Shoulder") then
		Character.Torso["Right Shoulder"].Part1 = nil
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Neck") then
		Character.Torso["Neck"].Part1 = nil
	end
	if Humanoid.Health <= 0 and EnableKnockback or EnableKnockback and KnockbackOnToggleRagdoll then
		local BodyVelocity = Instance.new("BodyVelocity", HumanoidRootPart)
		BodyVelocity.maxForce = Vector3.new(math.huge, 0, math.huge)
		BodyVelocity.P = 100000000000000
		BodyVelocity.Velocity = -HumanoidRootPart.CFrame.LookVector.unit * Knockback
		Debris:AddItem(BodyVelocity, 0.5)
	end
end

function Fix()
	local Head = Character:FindFirstChild("Head")
	local LeftArm = Character:FindFirstChild("Left Arm")
	local LeftLeg = Character:FindFirstChild("Left Leg")
	local RightArm = Character:FindFirstChild("Right Arm")
	local RightLeg = Character:FindFirstChild("Right Leg")
	local Torso = Character:FindFirstChild("Torso")
	if Character and Character:FindFirstChild("HumanoidRootPart") then
		Character.HumanoidRootPart.Massless = false
	end	
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Left Hip") and LeftLeg and not IsLimbDecapitated(LeftLeg) then
		Character.Torso["Left Hip"].Part1 = LeftLeg
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Right Hip") and RightLeg and not IsLimbDecapitated(RightLeg) then
		Character.Torso["Right Hip"].Part1 = RightLeg
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Neck") and Head and not IsLimbDecapitated(Head) then
		Character.Torso["Neck"].Part1 = Head
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Left Shoulder") and LeftArm and not IsLimbDecapitated(LeftArm) then
		Character.Torso["Left Shoulder"].Part1 = LeftArm
	end
	if Character and Character:FindFirstChild("Torso") and Character.Torso:FindFirstChild("Right Shoulder") and RightArm and not IsLimbDecapitated(RightArm) then
		Character.Torso["Right Shoulder"].Part1 = RightArm
	end
	local Object = Character:GetDescendants()
	for i = 1, #Object do
		if Object[i].Name == "Impact" or  Object[i].Name == "RagdollWeld" or Object[i].Name == "TorsoAttachment2" or  Object[i].Name == "TorsoAttachment3" or  Object[i].Name == "TorsoAttachment4" or  Object[i].Name == "TorsoAttachment5" or  Object[i].Name == "TorsoAttachment" or  Object[i].Name == "TorsoAttachment1" or   Object[i].Name == "Joint" or Object[i].Name == "BallSocketConstraint" or Object[i].Name == "HumanoidRootPartAttachment" or Object[i].Name == "LeftArmAttachment" or Object[i].Name == "RightArmAttachment" or Object[i].Name == "LeftLegAttachment" or Object[i].Name == "RightLegAttachment" or Object[i].Name == "HeadAttachment" then
			if not Object[i].Parent:FindFirstChild("Disabled") then
				Object[i]:Destroy()
			end
		elseif Object[i]:IsA("BallSocketConstraint") and not Object[i].Parent:FindFirstChild("Disabled") then
			Object[i]:Destroy()
		elseif Object[i]:IsA("Accessory") then
			Object[i].Handle.Massless = false
		end
	end
end

local function Toggle()
	if script.Activated.Value == true and Ragdolled == false and RagdolledPerm == false then 
		if not LivingRagdoll then
			Humanoid.PlatformStand = true
		else
			Humanoid.PlatformStand = false
		end
		if Character:FindFirstChild("CombatStates") then
			Character["CombatStates"]["Ragdolled"].Value = true
		end
		Ragdolled = true
		Ragdoll()
	elseif script.Activated.Value == false and Ragdolled == true then
		Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
		Humanoid.PlatformStand = false
		Ragdolled = false	
		if Character:FindFirstChild("CombatStates") then
			Character["CombatStates"]["Ragdolled"].Value = false
		end
		Fix()
	end
end

local function DestroyHumanoidRootPart()
	if Character:FindFirstChild("HumanoidRootPart") then
		Character.HumanoidRootPart:Destroy()
	end
end

Activated.Changed:Connect(Toggle)
Humanoid.Died:Connect(Ragdoll, RagdolledPerm == true)

script["StunDuration"].Changed:Connect(function()
	StunDuration = script["StunDuration"].Value
	if StunDuration > 0 then
		if not Ragdolled then
			if not LivingRagdoll then
				Humanoid.PlatformStand = true
			else
				Humanoid.PlatformStand = false
			end
			if Character:FindFirstChild("CombatStates") then
				Character["CombatStates"]["Ragdolled"].Value = true
			end
			Ragdolled = true
			Ragdoll()
		end
		if not Stunned then
			Stunned = true
			local LastTicked = tick()
			repeat Thread:Wait() until tick() - LastTicked >= StunDuration or not Ragdolled
			Humanoid.PlatformStand = false
			Ragdolled = false
			script.StunDuration.Value = 0
			Stunned = false
			if Character:FindFirstChild("CombatStates") then
				Character["CombatStates"]["Ragdolled"].Value = false
			end
			Fix()
		end
	end
end)

BUT, when the script unragdolling the player, I used Enum.HumanoidStateType.GettingUp and it didn’t work for me :frowning:

how i can fix that?

Maybe its a platform stand. Trying setting platform stand to false after you unragdoll.

I swapped them, rewrote and entered them, and in the end nothing happened(

fixed by using Enum.HumanoidStateType.GettingUp on the client side.