Ragdoll flinging when trying to get up

So this is my ragdoll code (code in ServerScriptService):

--||Services||--
local Players = game:GetService("Players")


--||Ragdoll CFrames (can be changed)||--
local attachmentCFrames = {
	["Neck"] = {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)},
	["Left Shoulder"] = {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 Shoulder"] = {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 Hip"] = {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)},
	["Right Hip"] = {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)},
}

--||Don't change this||--
local ragdollInstanceNames = {
	["RagdollAttachment"] = true,
	["RagdollConstraint"] = true,
	["ColliderPart"] = true,
}

------------------------------------------------------------------------------------------------------------------


task.wait()

--//Pushes the character
local function push(T)
	T:ApplyImpulse(T.CFrame.LookVector * 100)
end

--//Allows for some limb collisions
local function createColliderPart(part)
	if not part then return end
	
	local rp = Instance.new("Part")
	rp.Name = "ColliderPart"
	rp.Size = part.Size / 1.7
	rp.Massless = true
	rp.CFrame = part.CFrame
	rp.Transparency = 1
	rp.CollisionGroup = "NoPlayer"

	local wc = Instance.new("WeldConstraint")
	wc.Part0 = rp
	wc.Part1 = part
	wc.Parent = rp
	rp.Parent = part
end

--//Converts Motor6D's into BallSocketConstraints
local function replaceJoints(char, hum)
	hum.PlatformStand = true
	hum.AutoRotate = false
	char.HumanoidRootPart.Massless = true
	char.HumanoidRootPart.CanCollide = false
	
	for _, motor in pairs(char:GetDescendants()) do
		if motor:IsA("Motor6D") and attachmentCFrames[motor.Name] then
			motor.Enabled = false

			local a0, a1 = Instance.new("Attachment"), Instance.new("Attachment")
			a0.CFrame = attachmentCFrames[motor.Name][1]
			a1.CFrame = attachmentCFrames[motor.Name][2]

			a0.Name = "RagdollAttachment"
			a1.Name = "RagdollAttachment"

			createColliderPart(motor.Part1)

			local b = Instance.new("BallSocketConstraint")
			b.Attachment0 = a0
			b.Attachment1 = a1
			b.Name = "RagdollConstraint"

			b.Radius = 0.15
			b.LimitsEnabled = true
			b.TwistLimitsEnabled = false
			b.MaxFrictionTorque = 0
			b.Restitution = 0
			b.UpperAngle = 90
			b.TwistLowerAngle = -45
			b.TwistUpperAngle = 45

			if motor.Name == "Neck" then
				b.TwistLimitsEnabled = true
				b.UpperAngle = 45
				b.TwistLowerAngle = -70
				b.TwistUpperAngle = 70
			end

			a0.Parent = motor.Part0
			a1.Parent = motor.Part1
			b.Parent = motor.Parent
		end
	end
end

--//Destroys all Ragdoll made instances and re-enables the Motor6D's
local function resetJoints(hum)
	local char = hum.Parent

	char:SetAttribute("IsRagdoll", false)
	
	hum.PlatformStand = false
	hum.AutoRotate = true
	char.HumanoidRootPart.Massless = false
	char.HumanoidRootPart.CanCollide = true	

	if hum.Health < 1 then return end --we don't wanna reset the joints if the character is dead
	
	for _, instance in pairs(char:GetDescendants()) do
		if ragdollInstanceNames[instance.Name] then
			instance:Destroy()
		end
		if instance:IsA("Motor6D") then
			instance.Enabled = true
		end
	end
	
	hum:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
	hum:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
	hum:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
end

--//
local function ragdoll(char, hum)
	local isRagdoll = not char:GetAttribute("IsRagdoll")
	char:SetAttribute("IsRagdoll", isRagdoll)
end





--//Apply the Ragdoll stuff to every character with a humanoid in the workspace
local function applyRagdollToCharacters()
	for _, char in pairs(workspace:GetDescendants()) do
		local hum = char:FindFirstChild("Humanoid")
		if hum then
			local torso = char:FindFirstChild("Torso")
			if torso then
				hum.BreakJointsOnDeath = false
				hum.RequiresNeck = false
				
				char:GetAttributeChangedSignal("IsRagdoll"):Connect(function()
					local isRagdoll = char:GetAttribute("IsRagdoll")
					if isRagdoll then
						replaceJoints(char, hum)
					else
						resetJoints(hum)
					end
				end)

				hum.Died:Once(function()
					char:SetAttribute("IsRagdoll", true)
					char:SetAttribute("Stunned", true) --why not just stun them too :)
					push(torso)
				end)
			end
		end
	end
end

--//Run the function to apply the Ragdoll stuff
applyRagdollToCharacters()





--//for all players
Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		local hum = char:FindFirstChild("Humanoid")
		local torso = char:WaitForChild("Torso")

		hum.BreakJointsOnDeath = false
		hum.RequiresNeck = false

		char:GetAttributeChangedSignal("IsRagdoll"):Connect(function()
			local isRagdoll = char:GetAttribute("IsRagdoll")		
			if isRagdoll then
				replaceJoints(char, hum)
			else
				resetJoints(hum)
			end
		end)

		hum.Died:Once(function()
			char:SetAttribute("IsRagdoll", true)
			char:SetAttribute("Stunned", true) --why not just stun them too :)
			push(torso)
		end)
	end)
	
end)


The problem is that when the characters get up they kinda fling away, the interesting thing is that this is only happening in game, NOT in studio itself (at least for the npc’s but for players it’s always happening doesn’t matter if it’s in game or in studio).

Video of the problem:

But yea, I have no idea how I would fix that.

Does removing these lines fix the flinging issue?

Nope it doesn’t help, still the same

Update your resetJoints function with this

local function resetJoints(hum)
	local char = hum.Parent

	char:SetAttribute("IsRagdoll", false)
	
	-- Disable any forces or velocities
	for _, part in ipairs(char:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Velocity = Vector3.new()
			part.RotVelocity = Vector3.new()
		end
	end
	
	hum.PlatformStand = false
	hum.AutoRotate = true
	char.HumanoidRootPart.Massless = false
	char.HumanoidRootPart.CanCollide = true	

	if hum.Health < 1 then return end --we don't wanna reset the joints if the character is dead
	
	-- Adding a short delay
	task.wait(0.1)
	
	for _, instance in pairs(char:GetDescendants()) do
		if ragdollInstanceNames[instance.Name] then
			instance:Destroy()
		end
		if instance:IsA("Motor6D") then
			-- Manually synchronize CFrame before re-enabling
			local part0 = instance.Part0
			local part1 = instance.Part1
			if part0 and part1 then
				part1.CFrame = part0.CFrame * instance.C0 * instance.C1:Inverse()
			end
			instance.Enabled = true
		end
	end
	
	hum:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
	hum:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
	hum:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
end

now the character just lays on the ground when he gets up

Try setting the humanoid state to GettingUp when disabling the ragdoll

did you even look at the code? Doesnt seem like it.

I don’t see the part where you do that

in the reset joints function, which is the function for unragdolling

No, you’re setting that state to be enabled, not changing the humanoid to that state.

Isn’t that completely the same thing??

No. You have to use Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp) and im pretty sure it only works properly on the client