Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1.  
  2.  
  3. local NEVER_BREAK_JOINTS = false -- If you set this to true it will never break joints (this can create some welding issues, but can save stuff like hinges).
  4.  
  5.  
  6. local function CallOnChildren(Instance, FunctionToCall)
  7. -- Calls a function on each of the children of a certain object, using recursion.
  8.  
  9. FunctionToCall(Instance)
  10.  
  11. for _, Child in next, Instance:GetChildren() do
  12. CallOnChildren(Child, FunctionToCall)
  13. end
  14. end
  15.  
  16. local function GetNearestParent(Instance, ClassName)
  17. -- Returns the nearest parent of a certain class, or returns nil
  18.  
  19. local Ancestor = Instance
  20. repeat
  21. Ancestor = Ancestor.Parent
  22. if Ancestor == nil then
  23. return nil
  24. end
  25. until Ancestor:IsA(ClassName)
  26.  
  27. return Ancestor
  28. end
  29.  
  30. local function GetBricks(StartInstance)
  31. local List = {}
  32.  
  33. -- if StartInstance:IsA("BasePart") then
  34. -- List[#List+1] = StartInstance
  35. -- end
  36.  
  37. CallOnChildren(StartInstance, function(Item)
  38. if Item:IsA("BasePart") then
  39. List[#List+1] = Item;
  40. end
  41. end)
  42.  
  43. return List
  44. end
  45.  
  46. local function Modify(Instance, Values)
  47. -- Modifies an Instance by using a table.
  48.  
  49. assert(type(Values) == "table", "Values is not a table");
  50.  
  51. for Index, Value in next, Values do
  52. if type(Index) == "number" then
  53. Value.Parent = Instance
  54. else
  55. Instance[Index] = Value
  56. end
  57. end
  58. return Instance
  59. end
  60.  
  61. local function Make(ClassType, Properties)
  62. -- Using a syntax hack to create a nice way to Make new items.
  63.  
  64. return Modify(Instance.new(ClassType), Properties)
  65. end
  66.  
  67. local Surfaces = {"TopSurface", "BottomSurface", "LeftSurface", "RightSurface", "FrontSurface", "BackSurface"}
  68. local HingSurfaces = {"Hinge", "Motor", "SteppingMotor"}
  69.  
  70. local function HasWheelJoint(Part)
  71. for _, SurfaceName in pairs(Surfaces) do
  72. for _, HingSurfaceName in pairs(HingSurfaces) do
  73. if Part[SurfaceName].Name == HingSurfaceName then
  74. return true
  75. end
  76. end
  77. end
  78.  
  79. return false
  80. end
  81.  
  82. local function ShouldBreakJoints(Part)
  83. --- We do not want to break joints of wheels/hinges. This takes the utmost care to not do this. There are
  84. -- definitely some edge cases.
  85.  
  86. if NEVER_BREAK_JOINTS then
  87. return false
  88. end
  89.  
  90. if HasWheelJoint(Part) then
  91. return false
  92. end
  93.  
  94. local Connected = Part:GetConnectedParts()
  95.  
  96. if #Connected == 1 then
  97. return false
  98. end
  99.  
  100. for _, Item in pairs(Connected) do
  101. if HasWheelJoint(Item) then
  102. return false
  103. elseif not Item:IsDescendantOf(script.Parent) then
  104. return false
  105. end
  106. end
  107.  
  108. return true
  109. end
  110.  
  111. local function WeldTogether(Part0, Part1, JointType, WeldParent)
  112. --- Weld's 2 parts together
  113. -- @param Part0 The first part
  114. -- @param Part1 The second part (Dependent part most of the time).
  115. -- @param [JointType] The type of joint. Defaults to weld.
  116. -- @param [WeldParent] Parent of the weld, Defaults to Part0 (so GC is better).
  117. -- @return The weld created.
  118.  
  119. JointType = JointType or "Weld"
  120. local RelativeValue = Part1:FindFirstChild("qRelativeCFrameWeldValue")
  121.  
  122. local NewWeld = Part1:FindFirstChild("qCFrameWeldThingy") or Instance.new(JointType)
  123. Modify(NewWeld, {
  124. Name = "qCFrameWeldThingy";
  125. Part0 = Part0;
  126. Part1 = Part1;
  127. C0 = CFrame.new();--Part0.CFrame:inverse();
  128. C1 = RelativeValue and RelativeValue.Value or Part1.CFrame:toObjectSpace(Part0.CFrame); --Part1.CFrame:inverse() * Part0.CFrame;-- Part1.CFrame:inverse();
  129. Parent = Part1;
  130. })
  131.  
  132. if not RelativeValue then
  133. RelativeValue = Make("CFrameValue", {
  134. Parent = Part1;
  135. Name = "qRelativeCFrameWeldValue";
  136. Archivable = true;
  137. Value = NewWeld.C1;
  138. })
  139. end
  140.  
  141. return NewWeld
  142. end
  143.  
  144. local function WeldParts(Parts, MainPart, JointType, DoNotUnanchor)
  145. -- @param Parts The Parts to weld. Should be anchored to prevent really horrible results.
  146. -- @param MainPart The part to weld the model to (can be in the model).
  147. -- @param [JointType] The type of joint. Defaults to weld.
  148. -- @parm DoNotUnanchor Boolean, if true, will not unachor the model after cmopletion.
  149.  
  150. for _, Part in pairs(Parts) do
  151. if ShouldBreakJoints(Part) then
  152. Part:BreakJoints()
  153. end
  154. end
  155.  
  156. for _, Part in pairs(Parts) do
  157. if Part ~= MainPart then
  158. WeldTogether(MainPart, Part, JointType, MainPart)
  159. end
  160. end
  161.  
  162. if not DoNotUnanchor then
  163. for _, Part in pairs(Parts) do
  164. Part.Anchored = true
  165. end
  166. MainPart.Anchored = true
  167. end
  168. end
  169.  
  170. local function PerfectionWeld()
  171. local Tool = GetNearestParent(script, "Tool")
  172.  
  173. local Parts = GetBricks(script.Parent)
  174. local PrimaryPart = Tool and Tool:FindFirstChild("Handle") and Tool.Handle:IsA("BasePart") and Tool.Handle or script.Parent:IsA("Model") and script.Parent.PrimaryPart or Parts[1]
  175.  
  176. if PrimaryPart then
  177. WeldParts(Parts, PrimaryPart, "Weld", false)
  178. else
  179. warn("qWeld - Unable to weld part")
  180. end
  181.  
  182. return Tool
  183. end
  184.  
  185. local Tool = PerfectionWeld()
  186.  
  187.  
  188. if Tool and script.ClassName == "Script" then
  189. --- Don't bother with local scripts
  190.  
  191. script.Parent.AncestryChanged:connect(function()
  192. PerfectionWeld()
  193. end)
  194. end
  195.  
  196. -- Created by Quenty (@Quenty, follow me on twitter).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement