Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.40 KB | None | 0 0
  1. -- stolen from here <https://devforum.roblox.com/t/using-getassetidsforpackage-not-working/210592/13?u=call23re2> but fixed
  2.  
  3. local as = game:GetService('AssetService')
  4. local is = game:GetService('InsertService')
  5. local marketplaceService = game:GetService('MarketplaceService')
  6.  
  7.  
  8. local assetTypeDic = {
  9.     [27] = Enum.AssetType.Torso,
  10.     [28] = Enum.AssetType.RightArm,
  11.     [29] = Enum.AssetType.LeftArm,
  12.     [30] = Enum.AssetType.RightLeg,
  13.     [31] = Enum.AssetType.LeftLeg,
  14.     [17] = Enum.AssetType.Head,
  15.     [41] = Enum.AssetType.HairAccessory,
  16.     [42] = Enum.AssetType.FaceAccessory,
  17.     [43] = Enum.AssetType.NeckAccessory,
  18.     [44] = Enum.AssetType.ShoulderAccessory,
  19.     [45] = Enum.AssetType.FrontAccessory,
  20.     [46] = Enum.AssetType.BackAccessory,
  21.     [47] = Enum.AssetType.WaistAccessory,
  22.     [57] = Enum.AssetType.EarAccessory,
  23.     [58] = Enum.AssetType.EyeAccessory
  24. }
  25.  
  26. local function buildJoint(parentAttachment, partForJointAttachment)
  27.     local jointname = parentAttachment.Name:gsub("RigAttachment", "")
  28.     local motor = partForJointAttachment.Parent:FindFirstChild(jointname)
  29.     if not motor then
  30.         motor = Instance.new("Motor6D")
  31.     end
  32.     motor.Name = jointname
  33.  
  34.     motor.Part0 = parentAttachment.Parent
  35.     motor.Part1 = partForJointAttachment.Parent
  36.  
  37.     motor.C0 = parentAttachment.CFrame
  38.     motor.C1 = partForJointAttachment.CFrame
  39.  
  40.     motor.Parent = partForJointAttachment.Parent
  41. end
  42.  
  43. local function buildRigFromAttachments(last, part)
  44.     for _, attachment in pairs(part:GetChildren()) do
  45.         if attachment:IsA("Attachment") and string.find(attachment.Name, "RigAttachment") then
  46.             for _, sibling in pairs(part.Parent:GetChildren()) do
  47.                 if sibling ~= part and sibling ~= last then
  48.                     local matchingAttachment = sibling:FindFirstChild(attachment.Name)
  49.                     if matchingAttachment then
  50.                         buildJoint(attachment, matchingAttachment)
  51.                         buildRigFromAttachments(part, matchingAttachment.Parent)
  52.                     end
  53.                 end
  54.             end
  55.         end
  56.     end
  57. end
  58.  
  59. local function getOldCharacterMesh(character, newCharacterMesh)
  60.     for _, obj in pairs(character:GetChildren()) do
  61.         if obj:IsA("CharacterMesh") and obj.BodyPart == newCharacterMesh.BodyPart then
  62.             return obj
  63.         end
  64.     end
  65.     return nil
  66. end
  67. local function applyBodyPart(character, r15Folder, r6Folder)
  68.     local humanoid = character:FindFirstChild("Humanoid")
  69.     if humanoid.RigType == Enum.HumanoidRigType.R15 then
  70.         for _, part in pairs(r15Folder:GetChildren()) do
  71.             local oldPart = character:FindFirstChild(part.Name)
  72.             part:Clone().Parent = character
  73.             oldPart.Name = ""
  74.             oldPart:Destroy()
  75.         end
  76.         buildRigFromAttachments(nil, character.HumanoidRootPart)
  77.     else
  78.         for _, characterMesh in pairs(r6Folder:GetChildren()) do
  79.             local oldCharacterMesh = getOldCharacterMesh(character, characterMesh)
  80.             if oldCharacterMesh then
  81.                 oldCharacterMesh:Destroy()
  82.             end
  83.             characterMesh.Parent = character
  84.         end
  85.     end
  86. end
  87.  
  88. local function getAssetIds(bundleId)
  89.     local details = as:GetBundleDetailsAsync(bundleId)
  90.    
  91.     local ids = {}
  92.  
  93.     for i, detail in pairs(details) do
  94.         if type(detail) == "table" then
  95.             for _, d in pairs(detail) do
  96.                 if type(d) == "table" then
  97.                     for _, a in pairs(d) do
  98.                         if type(a) == "number" then
  99.                             local info = marketplaceService:GetProductInfo(a)
  100.                             if assetTypeDic[info.AssetTypeId] then
  101.                                 table.insert(ids, a)
  102.                             end
  103.                         end
  104.                     end
  105.                 end
  106.             end
  107.         end
  108.     end
  109.    
  110.     return ids
  111. end
  112.  
  113. function ImportPackage(packageId, char)
  114.     local humanoid = char:FindFirstChild("Humanoid")
  115.     local rigType = humanoid.RigType
  116.     if packageId ~= 1 then
  117.         local ids = getAssetIds(packageId)
  118.         for _, assetId in pairs(ids) do
  119.             local asset = is:LoadAsset(assetId)
  120.             if rigType == Enum.HumanoidRigType.R6 then
  121.                 if asset:FindFirstChild("R6") then
  122.                     applyBodyPart(char, nil, asset.R6)
  123.                 else
  124.                     if asset:FindFirstChildOfClass("Accessory") then
  125.                         local accessory = asset:FindFirstChildOfClass("Accessory")
  126.                         accessory.Parent = char
  127.                         humanoid:AddAccessory(accessory)
  128.                     elseif asset:FindFirstChildOfClass("Tool") then
  129.                         asset.Parent = char
  130.                     end
  131.                 end
  132.             else
  133.                 if asset:FindFirstChild("R15Fixed") then
  134.                     local folder = asset:FindFirstChild("R15Fixed")
  135.                     applyBodyPart(char, folder, nil)
  136.                 elseif asset:FindFirstChild("R15Anim") then
  137.                     asset:FindFirstChild("R15Anim").Parent = char
  138.                 elseif asset:FindFirstChild("Mesh") then
  139.                     char.Head:FindFirstChildOfClass("SpecialMesh"):Destroy()
  140.                     asset:FindFirstChild("Mesh").Parent = char:WaitForChild("Head")
  141.                 elseif asset:FindFirstChild("face") then
  142.                     char.Head:FindFirstChildOfClass("Decal"):Destroy()
  143.                     asset:FindFirstChild("face").Parent = char:WaitForChild("Head")
  144.                 else
  145.                     if asset:FindFirstChildOfClass("Accessory") then
  146.                         local a = asset:FindFirstChildOfClass("Accessory")
  147.                         a.Parent = char
  148.                         humanoid:AddAccessory(a)
  149.                     elseif asset:FindFirstChildOfClass("Tool") then
  150.                         asset:FindFirstChildOfClass("Tool").Parent = char
  151.                     elseif asset:FindFirstChildOfClass("Decal") then
  152.                         char.Head:FindFirstChildOfClass("Decal"):Destroy()
  153.                         asset:FindFirstChildOfClass("Decal").Parent = char.Head
  154.                     end
  155.                 end
  156.             end
  157.         end
  158.     end
  159.     humanoid.HipHeight = 1.35
  160. end
  161.  
  162. ImportPackage(451, workspace.Dummy) -- bundle id, path to character
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement