Ball Joints won't attach to player

RAGDOLL MODULE

local RagdollService = {}

ragdolle = game.ReplicatedStorage.Remotes.Ragdoll


statusModule = require(game.ServerScriptService.CombatFramework.Framework.StatusManager)
offsetdata = require(script.RagdollData)

PS = game:GetService("Players")

function RagdollService:SetupHumanoid(hum : Humanoid)
	hum.BreakJointsOnDeath = false
	hum.RequiresNeck = false
end

function RagdollService:BuildCollisionParts(char : Model)
	for _,v in pairs(char:GetDescendants()) do
		if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then
			local p = v:Clone()
			p.Parent = v
			p.CanCollide = false
			p.Massless = true
			p.Size = Vector3.one
			p.Name = "Collide"
			p.Transparency = 1
			p:ClearAllChildren()
			
			local weld = Instance.new("Weld")
			weld.Parent = p
			weld.Part0 = v
			weld.Part1 = p
		end
	end
end

function RagdollService:EnableCollisionParts(char : Model, enabled : boolean)
	for _,v in pairs(char:GetDescendants()) do
		if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" and v.Name ~= "Collide" then
			v.CanCollide = not enabled
			v.Collide.CanCollide = true
		end
end
end

function RagdollService:EnableMotor6D(char: Model, enabled: boolean)
	for _,v in pairs(char:GetDescendants()) do
		if v.Name == "Handle" or v.Name == "RootJoint" or v.Name == "Neck" then continue end
		if v:IsA("Motor6D") then v.Enabled = enabled end
		if v:IsA("BasePart") then v.CollisionGroup = if enabled then "Character" else "Ragdoll" end
	end
end

function RagdollService:BuildJoints(char: Model)
	local hrp = char:FindFirstChild("HumanoidRootPart")
	
	for _,v in pairs(char:GetDescendants()) do
		if v:IsA("Accessory") or v.Name == "Torso" or v.Name == "HumanoidRootPart" or v.Name == "Handle" then return end
		
		if not offsetdata[v.Name] then continue end
		local att0: Attachment, att1 : Attachment = Instance.new("Attachment"), Instance.new("Attachment")
		local joint: Constraint = game.ReplicatedStorage.RagdollJoints:FindFirstChild(offsetdata[v.Name].Joint):Clone()
		
		att0.Name = "RAGDOLL_ATTACHMENT"
		att0.Parent = v
		att0.CFrame = offsetdata[v.Name].CFrame[2]
		print(att0.Parent)
		
		att1.Name = "RAGDOLL_ATTACHMENT"
		att1.Parent = hrp
		att1.CFrame = offsetdata[v.Name].CFrame[1]
		
		joint.Name = "RAGDOLL_CONSTRAINT"
		joint.Parent = v
		joint.Attachment0 = att0
		joint.Attachment1 = att1
		
		v.Massless = true
		
	end
end

function RagdollService:DestroyJoints(char: Model)
	char.PrimaryPart.Massless = false
	
	for _,v in pairs(char:GetDescendants()) do
		if v.Name == "RAGDOLL_ATTACHMENT" or v.Name == "RAGDOLL_CONSTRAINT" then v:Destroy()
			
			if v:IsA("BasePart") or v:FindFirstAncestorOfClass("Accessory") or v.Name == "Torso" or v.Name == "Head" then continue end
		end
	end
end

function RagdollService:RagdollCharacter(char: Model, duration: number)
	if char:FindFirstChildOfClass("Humanoid"):GetState() == Enum.HumanoidStateType.Ragdoll then return end
	local charstatus = statusModule:RetriveStatus(char)
	charstatus.Misc = true
	
	
	local plr = PS:GetPlayerFromCharacter(char)
	local hum = char:FindFirstChildOfClass("Humanoid")
	local hrp = char:FindFirstChild("HumanoidRootPart")
	if not hrp then return end
	
	RagdollService:EnableMotor6D(char, false)
	RagdollService:BuildJoints(char)
	RagdollService:EnableCollisionParts(char, true)

	
	if plr then
		ragdolle:FireClient(plr, "Ragdoll")
	else
		hrp:SetNetworkOwner(nil)
		hum.AutoRotate = false
		hum.PlatformStand = true
	end
end

function RagdollService:UnragdollCharacter(char: Model, getUpState: number?)
	local charstatus = statusModule:RetriveStatus(char)
	charstatus.Misc = false
	local plr = PS:GetPlayerFromCharacter(char)
	local hum = char:FindFirstChildOfClass("Humanoid")
	local hrp = char:FindFirstChild("HumanoidRootPart")
	
	if plr then
	ragdolle:FireClient(plr, "Unragdoll")
	else
		hrp:SetNetworkOwner(nil)
		if hum:GetState() == Enum.HumanoidStateType.Dead then return end
		hum.PlatformStand = false
		hum:SetStateEnabled(Enum.HumanoidStateType.FallingDown, false)
		hum:ChangeState(Enum.HumanoidStateType.GettingUp)
	end
	
	
	RagdollService:DestroyJoints(char)
	RagdollService:EnableMotor6D(char, true)
	RagdollService:EnableCollisionParts(char, false)
	
	hum.AutoRotate = true
end

function RagdollService:Init()
	
	for _,v in pairs(workspace:GetDescendants()) do
		if v:IsA("Humanoid") then
			RagdollService:SetupHumanoid(v)
			RagdollService:BuildCollisionParts(v.Parent)
		end
	end
	
	PS.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(char)
			RagdollService:SetupHumanoid(char:WaitForChild("Humanoid"))
			RagdollService:BuildCollisionParts(char)
		end)
	end)
end
return RagdollService

RAGDOLL DATA

local offsets = {
	["Head"] = {
		Joint = "Neck",
		CFrame = {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)}
	},
	["HumanoidRootPart"] = {
		Joint = "HumanoidRootPart",
		CFrame = {CFrame.new(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), CFrame.new(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)}
	},
	["Right Arm"] = {
		Joint = "Default",
		CFrame = {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 Arm"] = {
		Joint = "Default",
		CFrame = {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 Leg"] = {
		Joint = "Default",
		CFrame = {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)}
	},
	["Left Leg"] = {
		Joint = "Default",
		CFrame = {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)}
	}
}

return offsets
1 Like