Levitating bug on my ragdoll module

  1. What do you want to achieve?
    I want to make a ragdoll system used for a simple combat game. Similar to a battleground game’s ragdoll system (“Strongest Battleground, Jujutsu shenanigans, etc.”)

  2. What is the issue?
    I have two issues, one the biggest and one pretty small. My main issue is often times when I jump before or after a ragdoll, I start to float upwards and unsure of the cause. I have a small issue where the player tends to flip out when getting up, being put in PlatformStand once again. This is a issue I may be able to figure out on my own but its unprioritized due to the main issue.

Video of problem:

  1. What solutions have you tried so far?
    I assume the Humanoid State is the cause due to it being in some sort of jumping or falling down state, causing it to ignore the initial changestate event, that hasn’t worked it. I believe I did it wrong on my end though.
local ragdoll = {}

local RunService = game:GetService("RunService")
local PhysicService = game:GetService("PhysicsService")

local shrinkAmount = 0.4

local PropertiesForCloneParts = {
	Transparency = 0.4,
	Massless = false,
	Anchored = false,
	CanCollide = true,
	CollisionGroup = "Ragdoll"
}

local ClonedParts = {}

function ragdoll.Start(character, duration)
	local humanoid = character:FindFirstChild("Humanoid")
	if not humanoid or humanoid.Health <= 0 then return end

	local isRagdolled = character:GetAttribute("Ragdoll")
	if isRagdolled == true then return end

	humanoid:ChangeState(Enum.HumanoidStateType.Physics)

	for _,track:AnimationTrack in pairs(humanoid:GetPlayingAnimationTracks()) do
		track:Stop()
	end
	
	character:SetAttribute("Ragdoll", true)
	
	ClonedParts[character.Name] = {}

	for _,part:BasePart in pairs(character:GetChildren()) do
		if part:IsA("Part") and part.Name ~= "HumanoidRootPart" then
	
			part.CollisionGroup = "Ragdoll"
			part.CanCollide = true
			
			--print(part.CanCollide)
				
			local clone = part:Clone()
			clone.Size = clone.Size - Vector3.new(shrinkAmount,shrinkAmount,shrinkAmount)
			clone.Parent = part
			clone.Name = clone.Name.."_Collider"

			for property, value in PropertiesForCloneParts do
				clone[property] = value
			end
	
			ClonedParts[character.Name][clone.Name] = clone
			local weld = Instance.new("Weld")
			weld.Part0 = part
			weld.Part1 = clone
			weld.Parent = clone

			local highlight = Instance.new("Highlight")
			highlight.Parent = clone
			
		end
	end

	for _, joint in pairs(character:GetDescendants()) do
		if joint:IsA("Motor6D") and joint.Name ~= "RootJoint" then
		
			local a0 = Instance.new("Attachment")
			local a1 = Instance.new("Attachment")
			a0.Name = "ragdoll0"
			a1.Name = "ragdoll1"
			a0.CFrame = joint.C0
			a1.CFrame = joint.C1
			a0.Parent = joint.Part0
			a1.Parent = joint.Part1

			local socket = Instance.new("BallSocketConstraint")
			socket.Name = "RagdollConstraint"
			socket.Attachment0 = a0
			socket.Attachment1 = a1
			socket.LimitsEnabled = true
			socket.TwistLimitsEnabled = true
			socket.Parent = joint.Parent

			joint.Enabled = false
		elseif joint.Name == "RootJoint" then
			local a0 = Instance.new("Attachment")
			local a1 = Instance.new("Attachment")
			a0.Name = "ragdoll0"
			a1.Name = "ragdoll1"
			a0.CFrame = joint.C0
			a1.CFrame = joint.C1
			a0.Parent = joint.Part0
			a1.Parent = joint.Part1

			local socket = Instance.new("BallSocketConstraint")
			socket.Name = "RagdollConstraint"
			socket.Attachment0 = a0
			socket.Attachment1 = a1
			socket.LimitsEnabled = false
			socket.TwistLimitsEnabled = false
			socket.Parent = joint.Parent
		end
	end
	
	
	
	
	humanoid.WalkSpeed = 0
	humanoid.JumpPower = 0
	humanoid.AutoRotate = false
	humanoid.PlatformStand = true
	
	humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)

	

	local connection
	connection = RunService.Heartbeat:Connect(function()
		if humanoid:GetState() ~= Enum.HumanoidStateType.PlatformStanding then
			humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
		end
	end)
	


	if duration then
		task.delay(duration, function()
			if character:GetAttribute("Ragdoll") == true then
				ragdoll.Stop(character)
				connection:Disconnect()
				connection = nil	
			end
		end)
	end
end

I am a novice to scripting and making this project out of a learning experience. So please feel free to tell me what I did wrong overall with my code and what I can do better if you like!