Advertisement
Kurokku

Untitled

Feb 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. local function buildJoint(parentAttachment, partForJointAttachment)
  2. local jointName = parentAttachment.Name:gsub("RigAttachment", "")
  3. local motor = partForJointAttachment.Parent:FindFirstChild(jointName)
  4. if not motor then
  5. motor = Instance.new("Motor6D")
  6. end
  7. motor.Name = jointName
  8.  
  9. motor.Part0 = parentAttachment.Parent
  10. motor.Part1 = partForJointAttachment.Parent
  11.  
  12. motor.C0 = parentAttachment.CFrame
  13. motor.C1 = partForJointAttachment.CFrame
  14.  
  15. motor.Parent = partForJointAttachment.Parent
  16. end
  17.  
  18. function buildRigFromAttachments(last, part)
  19. for _, attachment in pairs(part:GetChildren()) do
  20. if attachment:IsA("Attachment") and string.find(attachment.Name, "RigAttachment") then
  21. for _, sibling in pairs(part.Parent:GetChildren()) do
  22. if sibling ~= part and sibling ~= last then
  23. local matchingAttachment = sibling:FindFirstChild(attachment.Name)
  24. if matchingAttachment then
  25. buildJoint(attachment, matchingAttachment)
  26. buildRigFromAttachments(part, matchingAttachment.Parent)
  27. end
  28. end
  29. end
  30. end
  31. end
  32. end
  33.  
  34. local function getOldCharacterMesh(character, newCharacterMesh)
  35. for _, obj in pairs(character:GetChildren()) do
  36. if obj:IsA("CharacterMesh") and obj.BodyPart == newCharacterMesh.BodyPart then
  37. return obj
  38. end
  39. end
  40. return nil
  41. end
  42.  
  43. function applyBodyPart(character, r15Folder)
  44. local humanoid = character:FindFirstChild("Humanoid")
  45. if humanoid.RigType == Enum.HumanoidRigType.R15 then
  46. for _, part in pairs(r15Folder:GetChildren()) do
  47. local oldPart = character:FindFirstChild(part.Name)
  48. part:Clone().Parent = character
  49. oldPart.Name = ""
  50. oldPart:Destroy()
  51. end
  52. -- Rebuild the rig
  53. -- Note: if applying many body parts at once it is more efficient to call this after applying them all
  54. buildRigFromAttachments(nil, character.HumanoidRootPart)
  55. else
  56. for _, characterMesh in pairs(r15Folder:GetChildren()) do
  57. local oldCharacterMesh = getOldCharacterMesh(character, characterMesh)
  58. if oldCharacterMesh then
  59. oldCharacterMesh:Destroy()
  60. end
  61. characterMesh.Parent = character
  62. end
  63. end
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement