Flinging bug with ragdoll systems

Hey hey. My ragdoll system i’ve been testing around with works mostly but has one huge issue. When they ragdoll sometimes, they are fine, other times they go into outer space and their position is Vector3.new(nan, nan, nan).

I’m honestly not sure whats causing it. I’ve tried applying network ownership to the server, I’ve tried scrapping the ‘create-everything-at-once’ idea, I’ve tried setting parts to massless or not massless, changing humanoid state. I’ve gotten no progress and I have no idea how to debug this.

Here’s a video of it in action:

The code:

-- Services

local PhysicsService = game:GetService("PhysicsService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Modules
local Knit = require(ReplicatedStorage.Packages.Knit)

-- @service RagdollService.lua : KnitService
local RagdollService = Knit.CreateService({ Name = "RagdollService" })

function RagdollService:KnitStart()
	PhysicsService:RegisterCollisionGroup("Ragdolls")
	PhysicsService:RegisterCollisionGroup("Characters")
	PhysicsService:CollisionGroupSetCollidable("Ragdolls", "Characters", false)

	for _, player in pairs(Players:GetPlayers()) do
		local Character = player.Character or player.CharacterAdded:Wait()
		self:Setup(Character)
	end

	Players.PlayerAdded:Connect(function(Player)
		Player.CharacterAdded:Connect(function(Character)
			self:Setup(Character)
		end)
	end)
end

function RagdollService:Enable(Character)
	Character.Humanoid.PlatformStand = true
	Character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
	for _, constraint in pairs(Character:GetDescendants()) do
		if constraint:IsA("BasePart") then
			constraint.Velocity = Vector3.new()
			constraint.RotVelocity = Vector3.new()
			constraint.AssemblyAngularVelocity = Vector3.zero
			constraint.CollisionGroup = "Ragdolls"
		end
		if constraint:IsA("Motor6D") then
			constraint:Destroy()
		end
		if
			constraint:IsA("NoCollisionConstraint")
			or constraint:IsA("BallSocketConstraint")
			or constraint:IsA("HingeConstraint")
		then
			constraint.Enabled = true
		end
	end
end

function RagdollService:Setup(Character)
	if not Character then
		return
	end
	local Head = Character:WaitForChild("Head")
	local LeftArm = Character:WaitForChild("Left Arm")
	local LeftLeg = Character:WaitForChild("Left Leg")
	local RightArm = Character:WaitForChild("Right Arm")
	local RightLeg = Character:WaitForChild("Right Leg")
	local Torso = Character:WaitForChild("Torso")
	local Humanoid = Character:WaitForChild("Humanoid")

	Humanoid.BreakJointsOnDeath = false

	for _, part in pairs(Character:GetChildren()) do
		if part:IsA("BasePart") then
			part.CollisionGroup = "Characters"
		end
	end

	-- Head
	local NeckAttachment = Instance.new("Attachment")
	NeckAttachment.Name = "NeckAttachment"
	NeckAttachment.CFrame = CFrame.new(0, -Head.Size.Y / 2, 0)
	NeckAttachment.Parent = Head

	-- Left Arm
	local LeftShoulder = Instance.new("Attachment")
	LeftShoulder.Name = "LeftShoulder"
	LeftShoulder.CFrame = CFrame.new(0, 0.8, 0)
	LeftShoulder.Parent = LeftArm
	local LeftArmCollision = Instance.new("NoCollisionConstraint")
	LeftArmCollision.Enabled = false
	LeftArmCollision.Part0 = LeftArm
	LeftArmCollision.Part1 = LeftLeg
	LeftArmCollision.Parent = LeftArm

	-- Left Leg
	local LLegHingeAttachment = Instance.new("Attachment")
	LLegHingeAttachment.Name = "HingeAttachment"
	LLegHingeAttachment.CFrame = CFrame.new(0, 0.4, 0)
	LLegHingeAttachment.Parent = LeftLeg

	-- Right Arm
	local RightShoulder = Instance.new("Attachment")
	RightShoulder.Name = "RightShoulder"
	RightShoulder.CFrame = CFrame.new(0, 0.8, 0)
	RightShoulder.Parent = RightArm
	local RightArmCollision = Instance.new("NoCollisionConstraint")
	RightArmCollision.Enabled = false
	RightArmCollision.Part0 = RightArm
	RightArmCollision.Part1 = RightLeg
	RightArmCollision.Parent = RightArm

	-- Right Leg
	local RLegHingeAttachment = Instance.new("Attachment")
	RLegHingeAttachment.Name = "HingeAttachment"
	RLegHingeAttachment.CFrame = CFrame.new(0, 0.4, 0)
	RLegHingeAttachment.Parent = RightLeg

	-- Right Knee
	local RightKnee = Instance.new("Part")
	RightKnee.CanCollide = false
	RightKnee.CanTouch = false
	RightKnee.Massless = true
	RightKnee.Name = "Right Knee"
	RightKnee.Transparency = 1
	RightKnee.CFrame = RightLeg.CFrame
	RightKnee.Size = Vector3.new(1, 0.75, 1)
	RightKnee.Parent = Character
	local RKAttachment = Instance.new("Attachment")
	RKAttachment.Name = "Attachment0"
	RKAttachment.CFrame = CFrame.new(0, -0.375, 0)
	RKAttachment.Parent = RightKnee
	local RKWeldConstraint = Instance.new("WeldConstraint")
	RKWeldConstraint.Part0 = RightKnee
	RKWeldConstraint.Part1 = RightLeg
	RKWeldConstraint.Parent = RightKnee
	local RKHinge = Instance.new("HingeConstraint")
	RKHinge.Enabled = false
	RKHinge.LimitsEnabled = true
	RKHinge.LowerAngle = -60
	RKHinge.UpperAngle = 0
	RKHinge.Restitution = 1
	RKHinge.Attachment0 = RKAttachment
	RKHinge.Attachment1 = RLegHingeAttachment
	RKHinge.Parent = RightKnee
	local RightHip = Instance.new("Attachment")
	RightHip.Name = "Right Hip"
	RightHip.CFrame = CFrame.new(0, 0.375, 0)
	RightHip.Parent = RightKnee

	-- Left Knee
	local LeftKnee = Instance.new("Part")
	LeftKnee.CanCollide = false
	LeftKnee.CanTouch = false
	LeftKnee.Massless = true
	LeftKnee.Name = "Left Knee"
	LeftKnee.Transparency = 1
	LeftKnee.CFrame = LeftLeg.CFrame
	LeftKnee.Size = Vector3.new(1, 0.75, 1)
	LeftKnee.Parent = Character
	local LKAttachment = Instance.new("Attachment")
	LKAttachment.Name = "Attachment0"
	LKAttachment.CFrame = CFrame.new(0, -0.375, 0)
	LKAttachment.Parent = LeftKnee
	local LKWeldConstraint = Instance.new("WeldConstraint")
	LKWeldConstraint.Part0 = LeftKnee
	LKWeldConstraint.Part1 = LeftLeg
	LKWeldConstraint.Parent = LeftKnee
	local LKHinge = Instance.new("HingeConstraint")
	LKHinge.Enabled = false
	LKHinge.LimitsEnabled = true
	LKHinge.LowerAngle = -60
	LKHinge.UpperAngle = 0
	LKHinge.Restitution = 1
	LKHinge.Attachment0 = LKAttachment
	LKHinge.Attachment1 = LLegHingeAttachment
	LKHinge.Parent = LeftKnee
	local LeftHip = Instance.new("Attachment")
	LeftHip.Name = "Left Hip"
	LeftHip.CFrame = CFrame.new(0, 0.375, 0)
	LeftHip.Parent = LeftKnee

	-- Torso
	local LeftHipAttachment = Instance.new("Attachment")
	LeftHipAttachment.Name = "LeftHipAttachment"
	LeftHipAttachment.CFrame = CFrame.new(-0.5, -1, 0)
	LeftHipAttachment.Parent = Torso
	local LeftShoulderAttachment = Instance.new("Attachment")
	LeftShoulderAttachment.Name = "LeftShoulderAttachment"
	LeftShoulderAttachment.CFrame = CFrame.new(-1.25, 0.5, -0.2)
	LeftShoulderAttachment.Parent = Torso
	local NeckJointAttachment = Instance.new("Attachment")
	NeckJointAttachment.Name = "NeckJointAttachment"
	NeckJointAttachment.CFrame = CFrame.new(0, 1, 0)
	NeckJointAttachment.Parent = Torso
	local RightHipAttachment = Instance.new("Attachment")
	RightHipAttachment.Name = "RightHipAttachment"
	RightHipAttachment.CFrame = CFrame.new(0.5, -1, 0)
	RightHipAttachment.Parent = Torso
	local RightShoulderAttachment = Instance.new("Attachment")
	RightShoulderAttachment.Name = "RightShoulderAttachment"
	RightShoulderAttachment.CFrame = CFrame.new(1.25, 0.5, -0.2)
	RightShoulderAttachment.Parent = Torso

	local LeftHipSocket = Instance.new("BallSocketConstraint")
	LeftHipSocket.Enabled = false
	LeftHipSocket.LimitsEnabled = true
	LeftHipSocket.TwistLimitsEnabled = true
	LeftHipSocket.UpperAngle = 30
	LeftHipSocket.TwistLowerAngle = -30
	LeftHipSocket.TwistUpperAngle = 30
	LeftHipSocket.Restitution = 1
	LeftHipSocket.MaxFrictionTorque = 300
	LeftHipSocket.Attachment0 = LeftHipAttachment
	LeftHipSocket.Attachment1 = LeftHip
	LeftHipSocket.Parent = Torso
	local LeftShoulderSocket = Instance.new("BallSocketConstraint")
	LeftShoulderSocket.Enabled = false
	LeftShoulderSocket.LimitsEnabled = true
	LeftShoulderSocket.TwistLimitsEnabled = true
	LeftShoulderSocket.UpperAngle = 80
	LeftShoulderSocket.TwistLowerAngle = 0
	LeftShoulderSocket.TwistUpperAngle = 60
	LeftShoulderSocket.Restitution = 1
	LeftShoulderSocket.MaxFrictionTorque = 300
	LeftShoulderSocket.Attachment0 = LeftShoulder
	LeftShoulderSocket.Attachment1 = LeftShoulderAttachment
	LeftShoulderSocket.Parent = Torso
	local NeckSocket = Instance.new("BallSocketConstraint")
	NeckSocket.Enabled = false
	NeckSocket.LimitsEnabled = true
	NeckSocket.TwistLimitsEnabled = true
	NeckSocket.UpperAngle = 40
	NeckSocket.TwistLowerAngle = 5
	NeckSocket.TwistUpperAngle = -5
	NeckSocket.Restitution = 1
	NeckSocket.MaxFrictionTorque = 300
	NeckSocket.Attachment0 = NeckJointAttachment
	NeckSocket.Attachment1 = NeckAttachment
	NeckSocket.Parent = Torso
	local RightHipSocket = Instance.new("BallSocketConstraint")
	RightHipSocket.Enabled = false
	RightHipSocket.LimitsEnabled = true
	RightHipSocket.TwistLimitsEnabled = true
	RightHipSocket.UpperAngle = 30
	RightHipSocket.TwistLowerAngle = -30
	RightHipSocket.TwistUpperAngle = 30
	RightHipSocket.Restitution = 1
	RightHipSocket.MaxFrictionTorque = 300
	RightHipSocket.Attachment0 = RightHipAttachment
	RightHipSocket.Attachment1 = RightHip
	RightHipSocket.Parent = Torso
	local RightShoulderSocket = Instance.new("BallSocketConstraint")
	RightShoulderSocket.Enabled = false
	RightShoulderSocket.LimitsEnabled = true
	RightShoulderSocket.TwistLimitsEnabled = true
	RightShoulderSocket.UpperAngle = 60
	RightShoulderSocket.TwistLowerAngle = -60
	RightShoulderSocket.TwistUpperAngle = 0
	RightShoulderSocket.Restitution = 1
	RightShoulderSocket.MaxFrictionTorque = 300
	RightShoulderSocket.Attachment0 = RightShoulder
	RightShoulderSocket.Attachment1 = RightShoulderAttachment
	RightShoulderSocket.Parent = Torso
end

return RagdollService

This is how I call the code:

RagdollService:Enable(Character)

Before people discourage me from using Knit, I already know but this is a long time project so I can’t be bothered to change every single script away from Knit.

they’re not being flung, their humanoid root part is falling under the map. I had a similar issue with a ragdoll before, which was caused by the module not giving the hrp proper constraints. try skipping the hrp and not destroying it’s constraints in your ragdoll function.

Do the “Characters” collision group can collide with other CollisionGroups? (for example “Default”)

That was part of the issue but I found out the issue was me just creating the constraints multiple times in a different module that I forgot to remove beforehand. Thank you both though