How can I keep hats on when ragdolling?

for _,v in pairs(enemyChar:GetDescendants()) do 
	if not v:IsA("Accessory") then 
	       if v:IsA("Motor6D") then
				local a0, a1 = Instance.new("Attachment"), Instance.new("Attachment")
				a0.CFrame = v.C0
				a1.CFrame = v.C1 
				a0.Parent = v.Part0 
				a1.Parent = v.Part1 
										
				local b = Instance.new("BallSocketConstraint")
				b.Attachment0 = a1
				b.Attachment1 = a0 
				b.Parent = v.Part0 
											
				v.Part0 = nil 
				v.Part1 = nil
			end
		end
	end

Reattach the accessories the instant they fall off. I believe something like this roughly should do the trick. This code makes many assumptions, such as assuming that everything it requires exists.

for _, accessory in ipairs(enemyChar.Humanoid:GetAccessories()) do
    local handle = accessory.Handle
    local accessoryAttachment = handle:FindFirstChildOfClass("Attachment")
    local matchingAttachment = enemyChar:FindFirstChild(accessoryAttachment.Name, true)
    local limb = matchingAttachment:FindFirstAncestorWhichIsA("BasePart")

    local weld = Instance.new("Weld")
    weld.Name = "AccessoryWeld"
    weld.Part0 = handle
    weld.Part1 = limb
    weld.C0 = accessoryAttachment.CFrame
    weld.C1 = matchingAttachment.CFrame
    weld.Parent = weld.Part0
end

Anything along these lines will work. If you need to change the code to get rid of the assumptions it makes and instead account for unfulfilled requirements, that’s okay too. The magic is in the weld code.

Your requirements for completing the problem look like this:

  • Have a way to access all the character’s accessories, the accessory handles, the attachment in the accessory and the matching attachment as well as the parent limb in the character.

  • Create a weld between the accessory handle and the limb which owns the attachment, where the C0 and C1 are the respective CFrames of the attachments parented to Part0 and Part1.

2 Likes