Advertisement
tyridge77

Scale

Sep 12th, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.88 KB | None | 0 0
  1.  
  2. local Num = 0
  3. local Anchored = {}
  4.  
  5. function GetMesh(Obj)
  6.     for i,v in pairs(Obj:GetChildren()) do
  7.         if v:IsA("DataModelMesh") then
  8.             return v
  9.         end
  10.     end
  11. end
  12.  
  13. function GetCFrame(Obj)
  14.     if Obj == nil then
  15.         return
  16.     end
  17.     if Obj == Workspace then
  18.         return CFrame.new(0,0,0)
  19.     end
  20.     if Obj:IsA("Model") then
  21.         return Obj:GetModelCFrame()
  22.     end
  23.     if Obj:IsA("BasePart") then
  24.         return Obj.CFrame
  25.     end
  26.     return
  27. end
  28.  
  29. function GetSize(Obj)
  30.     if Obj == nil then
  31.         return
  32.     end
  33.     if Obj == Workspace then
  34.         return Vector3.new(0,0,0)
  35.     end
  36.     if Obj:IsA("Model") then
  37.         return Obj:GetModelSize()
  38.     end
  39.     if Obj:IsA("BasePart") then
  40.         return Obj.Size
  41.     end
  42.     return
  43. end
  44.  
  45. function SaveWelds(Obj,Welds,Scale)
  46.     if Welds == nil then
  47.         local Welds = {}
  48.         for i,v in pairs(Obj:GetChildren()) do
  49.             if v:IsA("JointInstance") then
  50.                 Welds[#Welds+1] = {v,v.Part0,v.Part1,v.C0,v.C1,v.Parent}
  51.                 v.Parent = nil
  52.             end
  53.             SaveWelds(v,Welds,Scale)
  54.         end
  55.         return Welds
  56.     else
  57.         for i,v in pairs(Obj:GetChildren()) do
  58.             if v:IsA("JointInstance") then
  59.                 Welds[#Welds+1] = {v,v.Part0,v.Part1,v.C0,v.C1,v.Parent}
  60.                 v.Parent = nil
  61.             end
  62.             SaveWelds(v,Welds,Scale)
  63.         end
  64.     end
  65. end
  66.  
  67. function GetMass(Obj)
  68.     local Mass = 0
  69.     if Obj:IsA("BasePart") then
  70.         Mass = Mass+Obj:GetMass()
  71.     end
  72.     for i,v in pairs(Obj:GetChildren()) do
  73.         Mass = Mass+GetMass(v)
  74.     end
  75.     return Mass
  76. end
  77.  
  78. function SaveConnectedParts(Obj,Welds)
  79.     if Welds == nil then
  80.         local Welds = {}
  81.         for i,v in pairs(Obj:GetChildren()) do
  82.             if v:IsA("BasePart") then
  83.                 for i2,v2 in pairs(v:GetConnectedParts()) do
  84.                     Welds[#Welds+1] = {Instance.new("Weld"),v,v2,v.CFrame:toObjectSpace(v2.CFrame),CFrame.new(0,0,0),v}
  85.                 end
  86.                 v:BreakJoints()
  87.             end
  88.             SaveConnectedParts(v,Welds)
  89.         end
  90.         return Welds
  91.     else
  92.         for i,v in pairs(Obj:GetChildren()) do
  93.             if v:IsA("BasePart") then
  94.                 for i2,v2 in pairs(v:GetConnectedParts()) do
  95.                     Welds[#Welds+1] = {Instance.new("Weld"),v,v2,v.CFrame:toObjectSpace(v2.CFrame),CFrame.new(0,0,0),v}
  96.                 end
  97.                 v:BreakJoints()
  98.             end
  99.             SaveConnectedParts(v,Welds)
  100.         end
  101.     end
  102. end
  103.  
  104. function ResizeWelds(Welds,Scale)
  105.     for i,v in pairs(Welds) do
  106.         v[1].Parent = v[6]
  107.         v[1].Part0 = v[2]
  108.         v[1].Part1 = v[3]
  109.         local Vec = v[4].p
  110.         local Vec2 = v[5].p
  111.         v[1].C0 = CFrame.new(Vec*Scale)*(v[4]-Vec)
  112.         v[1].C1 = CFrame.new(Vec2*Scale)*(v[5]-Vec2)
  113.     end
  114. end
  115.  
  116. function AnchorParts(Obj)
  117.     for i,v in pairs(Obj:GetChildren()) do
  118.         if v:IsA("BasePart") and v.Anchored == false then
  119.             v.Anchored = true
  120.             Anchored[v] = v
  121.         end
  122.         AnchorParts(v)
  123.     end
  124. end
  125.  
  126. function ResizeFunc(Obj,Scale,Welds,Center)
  127.     Center = Center or CFrame.new(0,0,0)
  128.     for i,v in pairs(Obj:GetChildren()) do
  129.         if v:IsA("BasePart") then
  130.             --[[Num = Num+1
  131.             if Num == 100 then
  132.                 wait(0.25)
  133.                 Num = 0
  134.             end]]
  135.             local Size = v.Size
  136.             for i2,v2 in pairs(v:GetChildren()) do
  137.                 if v2.Name == "ScaleInserted" then
  138.                     Size = Size*v2.Scale
  139.                     v2:Destroy()
  140.                 elseif v2.Name:sub(1,14) == "ScaleInserted:" then
  141.                     local X,Y,Z;
  142.                     for Match in v2.Name:sub(15):gmatch("[^,%s]+") do
  143.                         if Y ~= nil then
  144.                             Z = tonumber(Match)
  145.                         elseif X ~= nil then
  146.                             Y = tonumber(Match)
  147.                         else
  148.                             X = tonumber(Match)
  149.                         end
  150.                     end
  151.                     Size = Vector3.new(X,Y,Z)
  152.                     v2:Destroy()
  153.                 end
  154.             end
  155.             pcall(function() v.FormFactor = "Custom" end)
  156.             local CFr = v.CFrame
  157.             local Want = Size*Scale
  158.             v.Size = Want
  159.             if v:IsA("VehicleSeat") then
  160.                 Seats[#Seats+1] = v
  161.             end
  162.             if v.Size ~= Want then
  163.                 local Name = ""
  164.                 for Match in v.Name:gmatch("[^%s]+") do
  165.                     Name = Name..Match
  166.                 end
  167.                 local CharMesh = nil
  168.                 for i,v in pairs(Obj:GetChildren()) do
  169.                     if v:IsA("CharacterMesh") and tostring(v.BodyPart):sub(15) == Name then
  170.                         CharMesh = v
  171.                     end
  172.                 end
  173.                 if CharMesh == nil then
  174.                     local Mesh = GetMesh(v)
  175.                     if Mesh == nil then
  176.                         local Mesh;
  177.                         if v:IsA("WedgePart") then
  178.                             Mesh = Instance.new("SpecialMesh",v)
  179.                             Mesh.MeshType = "Wedge"
  180.                         else
  181.                             local HasShape = pcall(function() return v.Shape end)
  182.                             if HasShape then
  183.                                 if v.Shape == "Ball" then
  184.                                     Mesh = Instance.new("SpecialMesh",v)
  185.                                     Mesh.MeshType = "Sphere"
  186.                                 elseif v.Shape == "Cylinder" then
  187.                                     Mesh = Instance.new("SpecialMesh",v)
  188.                                     Mesh.MeshType = "Cylinder"
  189.                                 else
  190.                                     Mesh = Instance.new("BlockMesh",v)
  191.                                 end
  192.                             else
  193.                                 Mesh = Instance.new("BlockMesh",v)
  194.                             end
  195.                         end
  196.                         Mesh.Scale = Want/v.Size
  197.                         Mesh.Name = "ScaleInserted"
  198.                     else
  199.                         if (Mesh.ClassName == "SpecialMesh" and Mesh.MeshType ~= Enum.MeshType.FileMesh) or Mesh.ClassName ~= "SpecialMesh" then
  200.                             Mesh.Scale = Want/v.Size*Mesh.Scale
  201.                         end
  202.                     end
  203.                 else
  204.                     local Mesh = Instance.new("SpecialMesh",v)
  205.                     Mesh.Name = "ScaleInserted:"..tostring(Want)
  206.                     Mesh.MeshType = "FileMesh"
  207.                     Mesh.MeshId = "rbxassetid://"..CharMesh.MeshId
  208.                     --Mesh.TextureId = CharMesh.BaseTextureId
  209.                     Mesh.Scale = Vector3.new(1,1,1)*Want.X/Scale
  210.                 end
  211.             end
  212.             v:BreakJoints()
  213.             local Rel = Center:toObjectSpace(CFr)
  214.             local New = CFrame.new(Rel.p*Scale)*(Rel-Rel.p)
  215.             v.CFrame = Center*New
  216.             v:BreakJoints()
  217.         elseif v:IsA("DataModelMesh") then
  218.             if v.ClassName == "SpecialMesh" then
  219.                 if v.MeshType == Enum.MeshType.FileMesh then
  220.                     v.Scale = v.Scale*Scale
  221.                 end
  222.             else
  223.                 --v.Scale = v.Scale*Scale
  224.             end
  225.         end
  226.         ResizeFunc(v,Scale,Welds,Center)
  227.     end
  228. end
  229.  
  230. function Resize(Obj,Scale,Break)
  231.     local Welds = SaveWelds(Obj,nil,Scale)
  232.     SaveConnectedParts(Obj,Welds)
  233.     local Mass = GetMass(Obj)
  234.     local Center = GetCFrame(Obj)
  235.     if Center ~= nil and Workspace:FindFirstChild("Base") ~= nil then
  236.         --Center = Center-Vector3.new(0,GetSize(Obj).Y/2,0)
  237.         Center = CFrame.new(Center.X,Workspace.Base.Position.Y+Workspace.Base.Size.Y/2,Center.Z)*(Center-Center.p)
  238.     end
  239.     Seats = {}
  240.     ResizeFunc(Obj,Scale,Welds,Center)
  241.     ResizeWelds(Welds,Scale)
  242.     local Mass2 = GetMass(Obj)
  243.     for i,v in pairs(Seats) do
  244.         v.Torque = Mass2*(v.Torque/Mass)
  245.         v.TurnSpeed = v.TurnSpeed*Scale
  246.         v.MaxSpeed = v.MaxSpeed*Scale
  247.     end
  248. end
  249.  
  250. game.Players.tyridge77.Chatted:connect(function(Msg)
  251.     if Msg:sub(1,4) == ";ws " then
  252.         local Name,Number = Msg:sub(5):match("(.+) ([%d%.e]+)")
  253.         local Anchor = Msg:sub(5):match(".+ .+ (.+)") == nil
  254.         if Name ~= nil and Num ~= nil and Workspace:FindFirstChild(Name) ~= nil and tonumber(Number) ~= nil then   
  255.             print("Scaling "..Workspace[Name]:GetFullName().." to "..tonumber(Number))
  256.             --Num = 0
  257.             Anchored = {}
  258.             AnchorParts(Workspace[Name])
  259.             Resize(Workspace[Name],tonumber(Number),true)
  260.             Workspace[Name]:MakeJoints()
  261.             if not Anchor then
  262.                 wait()
  263.                 for i,v in pairs(Anchored) do
  264.                     v.Velocity = Vector3.new(0,0,0)
  265.                     v.RotVelocity = Vector3.new(0,0,0)
  266.                     v.Anchored = false
  267.                 end
  268.             end
  269.             Anchored = {}
  270.         end
  271.     end
  272. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement