local function t(t,K,s)local o=t:FindFirstChild("\072\117\109\097\110\111\105\100")if o.RigType==Enum.HumanoidRigType.R15 then for K,s in pairs(K:GetChildren())do local o=t:FindFirstChild(s.Name)if o then(s:Clone()).Parent=t o.Name=""o:Destroy()end end buildRigFromAttachments(nil,t.HumanoidRootPart)else for K,s in pairs(s:GetChildren())do local o=getOldCharacterMesh(t,s)if o then o:Destroy()end s.Parent=t end end end local K=game:GetService("\065\115\115\101\116\083\101\114\118\105\099\101")local s=game:GetService("\073\110\115\101\114\116\083\101\114\118\105\099\101")function DressCharacter(o,a,j)local q=K:GetAssetIdsForPackage(a)local p=o:FindFirstChild("\072\117\109\097\110\111\105\100")j=j or false for K,a in pairs(q)do local q=s:LoadAsset(a)for K,s in pairs(q:GetChildren())do if s:IsA("\070\111\108\100\101\114")and s.Name=="\082\049\053"then t(o,s,nil)elseif s:IsA("\084\111\111\108")and j then p:EquipTool(s)elseif s:IsA("\065\099\099\101\115\115\111\114\121")or s:IsA("\072\097\116")then p:AddAccessory(s)end end q:Destroy()end return true end return DressCharacter

local function applyBodyPart(character, r15Folder, r6Folder)
    local humanoid = character:FindFirstChild("Humanoid")
    if humanoid.RigType == Enum.HumanoidRigType.R15 then
		for _, part in pairs(r15Folder:GetChildren()) do
            local oldPart = character:FindFirstChild(part.Name)
			if (oldPart) then
	            part:Clone().Parent = character
	            oldPart.Name = ""
	            oldPart:Destroy()
			end
        end
        -- Rebuild the rig
        -- Note: if applying many body parts at once it is more efficient to call this after applying them all
        buildRigFromAttachments(nil, character.HumanoidRootPart)
    else
        for _, characterMesh in pairs(r6Folder:GetChildren()) do
            local oldCharacterMesh = getOldCharacterMesh(character, characterMesh)
            if oldCharacterMesh then
                oldCharacterMesh:Destroy()
            end
            characterMesh.Parent = character
        end
    end
end

local AssetService = game:GetService("AssetService")
local InsertService = game:GetService("InsertService")

function DressCharacter(character, packageId, allowTools)
	local PackageIds = AssetService:GetAssetIdsForPackage(packageId)
	local Humanoid = character:FindFirstChild("Humanoid")
	allowTools = (allowTools or false)
	
	for _,AssetId in pairs(PackageIds) do
		local Insert = InsertService:LoadAsset(AssetId)
		
		for _,Object in pairs(Insert:GetChildren()) do
			if (Object:IsA("Folder") and Object.Name == "R15") then
				applyBodyPart(character, Object, nil)
			elseif (Object:IsA("Tool") and allowTools) then
				Humanoid:EquipTool(Object)
			elseif (Object:IsA("Accessory") or Object:IsA("Hat")) then
				Humanoid:AddAccessory(Object)
			end
		end
		
		Insert:Destroy()
	end
	
	return true
end

return DressCharacter
