Advertisement
TaylorsRus

Untitled

Apr 17th, 2024
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. --// Point of the module is to offer clean and seamless transformation between two models.
  2.  
  3. local function ScaleModel(Model, TargetModel)
  4.     local Humanoid, HumRP = Model:WaitForChild("Humanoid"), Model:WaitForChild("HumanoidRootPart")
  5.     local Torso = Model:WaitForChild("Torso")
  6.     local NewTorso = TargetModel:WaitForChild("Torso")
  7.  
  8.     local CharacterSize, NewModelSize = Torso.Size, NewTorso:GetExtentsSize()
  9.  
  10.     local PrimaryCFrame = Torso.CFrame
  11.     local Scale = NewModelSize.Y / CharacterSize.Y
  12.  
  13.     Humanoid.HipHeight *= Scale
  14.     Scale = Vector3.one * Scale
  15.  
  16.     for _,Component in ipairs(Model:GetDescendants()) do
  17.         if Component:IsA("Part") then
  18.             Component.Size *= Scale
  19.  
  20.         elseif Component:IsA("Motor6D") and Component.Parent ~= HumRP then
  21.             Component.C0 = CFrame.new(Component.C0.p * Scale) * (Component.C0 - Component.C0.p)
  22.             Component.C1 = CFrame.new(Component.C1.p * Scale) * (Component.C1 - Component.C1.p)
  23.  
  24.         elseif Component:IsA("Attachment") then
  25.             Component.Position *= Scale
  26.         end
  27.     end
  28. end
  29.  
  30. local function ChangeModelTransparency(Model, Value)
  31.     for _,Component in Model:GetDescendants() do
  32.         if not Component:IsA("Part") then
  33.             continue
  34.         end
  35.        
  36.         Component.Transparency = Value
  37.     end
  38. end
  39.  
  40. return function (Model, TargetModel)
  41.     local Humanoid, HumRP = Model:WaitForChild("Humanoid"), Model:WaitForChild("HumanoidRootPart")
  42.    
  43.     --// First, we scale the "Model" to the size of the "TargetModel"
  44.     ScaleModel(Model, TargetModel)
  45.     ChangeModelTransparency(Model, 1)
  46.    
  47.     --// Then, we weld all the pieces of TargetModel, onto Model, like a morph.
  48.     for _,Limb in TargetModel:GetChildren() do
  49.         --// Every model consists of sub-models which are named after the limbs.
  50.         if not Limb:IsA("Model") then
  51.             continue
  52.         end
  53.         Limb = Limb:Clone()
  54.        
  55.         local Root = Limb.PrimaryPart
  56.         local TargetLimb = Model:FindFirstChild(Limb.Name)
  57.         if not TargetLimb then
  58.             error("Couldn't find target limb: "..TargetModel.Name)
  59.         end
  60.        
  61.         Limb:PivotTo(TargetLimb.CFrame)
  62.         Limb.Parent = TargetLimb
  63.        
  64.         local WeldConstraint = Instance.new("WeldConstraint")
  65.         WeldConstraint.Part0, WeldConstraint.Part1 = TargetLimb, Root
  66.         WeldConstraint.Parent = Root
  67.     end
  68.    
  69.     --// Guard clause to ensure our new model is fully visible.
  70.     ChangeModelTransparency(TargetModel, 0)
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement