Need help with ragdoll system

I made a ragdoll script which allows me to ragdoll and unragdoll the player using a value.
The player isnt ragdolling correctly, as he should fall down to the ground, instead he kind of flies/levitates and gets flinged up. no errors in output. any fix?

local plrs = game.Players

local jointsCache = {}

local function GetMotor6Ds(character: Model)
	local constraints = {}
	for i,v in pairs(character:GetDescendants()) do
		if v:IsA("Motor6D") then
			table.insert(constraints,v)
		end
	end
	return constraints
end

local function Ragdoll(character)
	local humanoid:Humanoid = character.Humanoid

	humanoid:SetStateEnabled(Enum.HumanoidStateType.Physics,true)
	for _,motor6d: Motor6D in pairs(jointsCache[character]) do
		motor6d.Enabled = false
		local a0,a1 = Instance.new("Attachment"),Instance.new("Attachment")
		a0.CFrame = motor6d.C0
		a0.Parent = motor6d.Part0

		a1.CFrame = motor6d.C1
		a1.Parent = motor6d.Part1

		local socket = Instance.new("BallSocketConstraint")
		socket.Attachment0 = a0
		socket.Attachment1 = a1

		if motor6d.Name == "Neck" then
			socket.LimitsEnabled = true
			socket.TwistLimitsEnabled = true
			socket.UpperAngle = 10
		end

		socket.Parent = motor6d.Parent
	end
	humanoid.WalkSpeed = 0
	humanoid.JumpPower = 0
	jointsCache[character] = nil
end

local function UnRagdoll(character)
	local humanoid:Humanoid = character.Humanoid

	humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll,false)
	for _,ballSocket: BallSocketConstraint in pairs(jointsCache[character]) do
		ballSocket:Destroy()
	end
	
	for _,motor6D : Motor6D in pairs(character:GetDescendants()) do
		motor6D.Enabled = true
	end
	jointsCache[character] = GetMotor6Ds(character)	
	humanoid.WalkSpeed = 16
	humanoid.JumpPower = 16
end

plrs.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(chr)
		if chr:GetAttribute("Character") then
			local hum: Humanoid = chr:FindFirstChild("Humanoid")
			hum.BreakJointsOnDeath = false
			hum.Died:Connect(function()
				Ragdoll(chr)
			end)
			jointsCache[chr] = GetMotor6Ds(chr)	
			local isRagdoll: BoolValue = chr:FindFirstChild("IsRagdoll")
			if isRagdoll then
				isRagdoll.Changed:Connect(function(value)
					if value == false then
						UnRagdoll(chr)
					else
						Ragdoll(chr)
					end
				end)
			end	
		end
	end)
end)
External Media

Make sure to set state on the client since client has control over humanoid state and movement.

Or you can set Platformstand to true and that should automatically replicate to the client.

Thank you! I just have a question, is it possible to make the arms and legs be able to collide so the arms and legs dont clip into the ground?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.