Advertisement
chocken123

Roblox model to part source code

Jul 13th, 2020 (edited)
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.58 KB | None | 0 0
  1. -- Created by chocken123
  2.  
  3. local Toolbar = plugin:CreateToolbar("Model to part")
  4. local MainButton = Toolbar:CreateButton("Create part", "Creates a new part which is the size of the model's boundaries and located at the centre of its mass ", "rbxasset://textures/TerrainTools/icon_shape_cube.png")
  5. local SettingsButton = Toolbar:CreateButton("Part settings", "Choose type of part, material, color and name (in development)", "rbxasset://textures/GameSettings/ToolbarIcon.png")
  6. local ChangeHistoryService = game:GetService("ChangeHistoryService")
  7. local PartType = "Block"
  8. local PartMaterial = Enum.Material.Plastic
  9. local PartName = "Part"
  10. local FitBall = false
  11. local PartColor3
  12. local PartBrickColor
  13. local UsingColor3
  14. local PartTypeStrings = "Block\nWedge\nWedgePart\nBall\nMeshPart\nCylinder\nSpawnLocation"
  15. local HelpMessage = "-----\nPart type - type of part to fill model e.g. Ball, Cylinder or Wedge\nFit ball - Makes sure ball part type size fits into the model\nSize increment - increases the part size by set Vector3\nSize decrease - decreases the part size by set Vector3\n-----"
  16. local SizeIncrement = Vector3.new(0,0,0)
  17. local SizeDecrease = Vector3.new(0,0,0)
  18. local Order = 0
  19.  
  20. local widgetInfo = DockWidgetPluginGuiInfo.new(
  21.     Enum.InitialDockState.Float,  -- Widget will be initialized in floating panel
  22.     false,   -- Widget will be initially enabled
  23.     true,  -- Don't override the previous enabled state
  24.     200,    -- Default width of the floating window
  25.     200,    -- Default height of the floating window
  26.     50,    -- Minimum width of the floating window (optional)
  27.     50     -- Minimum height of the floating window (optional)
  28. )
  29.  
  30. -- Create new widget GUI
  31. local SettingsWidget = plugin:CreateDockWidgetPluginGui("Part settings", widgetInfo)
  32. SettingsWidget.Title = "Part Settings"
  33. SettingsWidget.Enabled = false
  34.  
  35.  
  36. -- Ui object environment creation
  37.  
  38.  
  39. local LeftSide = {}
  40. local RightSide = {}
  41.  
  42. local MainFrame = Instance.new("Frame")
  43. MainFrame.Size = UDim2.new(1,0,0.9,0)
  44. MainFrame.BackgroundColor3 = Color3.new(1,1,1)
  45. MainFrame.BorderSizePixel = 0
  46.  
  47. --local Layout = Instance.new('UIListLayout')
  48. --Layout.Parent = MainFrame
  49.  
  50. local UILayout = Instance.new("UIGridLayout")
  51. UILayout.Parent = MainFrame
  52. UILayout.CellPadding = UDim2.new(0,0,0,0)
  53. UILayout.CellSize = UDim2.new(0.5,0,0.1,0)
  54. UILayout.SortOrder = Enum.SortOrder.LayoutOrder
  55.  
  56. local PartTypeBtn = Instance.new("TextButton")
  57. PartTypeBtn.Text = "Set part type as:"
  58. PartTypeBtn.BorderSizePixel = 0
  59. PartTypeBtn.TextSize = 13
  60. table.insert(LeftSide,PartTypeBtn)
  61. PartTypeBtn.LayoutOrder = Order
  62. Order = Order + 1
  63.  
  64. local PartTypeBox = Instance.new("TextBox")
  65. PartTypeBox.Text = "Block"
  66. PartTypeBox.ClearTextOnFocus = false
  67. PartTypeBox.BorderSizePixel = 0
  68. PartTypeBox.TextSize = 13
  69. table.insert(RightSide,PartTypeBox)
  70. PartTypeBox.LayoutOrder = Order
  71. Order = Order + 1
  72.  
  73.  
  74.  
  75.  
  76. local FitBall = Instance.new("TextButton")
  77. FitBall.Text = "Fit ball"
  78. FitBall.BorderSizePixel = 0
  79. PartTypeBtn.TextSize = 13
  80. table.insert(LeftSide, FitBall)
  81. FitBall.LayoutOrder = Order
  82. Order = Order + 1
  83.  
  84. local PrintSupportedTypes = Instance.new("TextButton")
  85. PrintSupportedTypes.Text = "Print supported part types"
  86. PrintSupportedTypes.BorderSizePixel = 0
  87. PartTypeBtn.TextSize = 13
  88. table.insert(RightSide, PrintSupportedTypes)
  89. PrintSupportedTypes.LayoutOrder = Order
  90. Order = Order + 1
  91.  
  92. local IncrementSize = PartTypeBtn:Clone()
  93. IncrementSize.Text = "Set increment of:"
  94. table.insert(LeftSide, IncrementSize)
  95. IncrementSize.LayoutOrder = Order
  96. Order = Order + 1
  97.  
  98. local IncrementSizeFrame = Instance.new("Frame")
  99. IncrementSizeFrame.BorderSizePixel = 0
  100. table.insert(RightSide, IncrementSizeFrame)
  101. IncrementSizeFrame.LayoutOrder = Order
  102. Order = Order + 1
  103.  
  104. local IncrementSizeBox1 = Instance.new("TextBox")
  105. IncrementSizeBox1.Size = UDim2.new((1/3),0,1,0)
  106. IncrementSizeBox1.Text = "0"
  107. IncrementSizeBox1.TextSize = 13
  108. IncrementSizeBox1.BackgroundTransparency = 1
  109. IncrementSizeBox1.ClearTextOnFocus = false
  110. IncrementSizeBox1.Parent = IncrementSizeFrame
  111.  
  112. local IncrementSizeBox2 = Instance.new("TextBox")
  113. IncrementSizeBox2.Size = UDim2.new((1/3),0,1,0)
  114. IncrementSizeBox2.Text = "0"
  115. IncrementSizeBox2.Position = UDim2.new((1/3),0,0,0)
  116. IncrementSizeBox2.TextSize = 13
  117. IncrementSizeBox2.BackgroundTransparency = 1
  118. IncrementSizeBox2.ClearTextOnFocus = false
  119. IncrementSizeBox2.Parent = IncrementSizeFrame
  120.  
  121. local IncrementSizeBox3 = Instance.new("TextBox")
  122. IncrementSizeBox3.Size = UDim2.new((1/3),0,1,0)
  123. IncrementSizeBox3.Text = "0"
  124. IncrementSizeBox3.Position = UDim2.new((2/3),0,0,0)
  125. IncrementSizeBox3.TextSize = 13
  126. IncrementSizeBox3.ClearTextOnFocus = false
  127. IncrementSizeBox3.BackgroundTransparency = 1
  128. IncrementSizeBox3.Parent = IncrementSizeFrame
  129.  
  130. local DecreaseSize = PartTypeBtn:Clone()
  131. DecreaseSize.Text = "Set decrease of:"
  132. table.insert(LeftSide, DecreaseSize)
  133. DecreaseSize.LayoutOrder = Order
  134. Order = Order + 1
  135.  
  136. local DecreaseSizeFrame = Instance.new("Frame")
  137. DecreaseSizeFrame.BorderSizePixel = 0
  138. table.insert(RightSide, DecreaseSizeFrame)
  139. DecreaseSizeFrame.LayoutOrder = Order
  140. Order = Order + 1
  141.  
  142. local DecreaseSizeBox1 = Instance.new("TextBox")
  143. DecreaseSizeBox1.Size = UDim2.new((1/3),0,1,0)
  144. DecreaseSizeBox1.Text = "0"
  145. DecreaseSizeBox1.TextSize = 13
  146. DecreaseSizeBox1.BackgroundTransparency = 1
  147. DecreaseSizeBox1.ClearTextOnFocus = false
  148. DecreaseSizeBox1.Parent = DecreaseSizeFrame
  149.  
  150. local DecreaseSizeBox2 = Instance.new("TextBox")
  151. DecreaseSizeBox2.Size = UDim2.new((1/3),0,1,0)
  152. DecreaseSizeBox2.Text = "0"
  153. DecreaseSizeBox2.Position = UDim2.new((1/3),0,0,0)
  154. DecreaseSizeBox2.TextSize = 13
  155. DecreaseSizeBox2.BackgroundTransparency = 1
  156. DecreaseSizeBox2.ClearTextOnFocus = false
  157. DecreaseSizeBox2.Parent = DecreaseSizeFrame
  158.  
  159. local DecreaseSizeBox3 = Instance.new("TextBox")
  160. DecreaseSizeBox3.Size = UDim2.new((1/3),0,1,0)
  161. DecreaseSizeBox3.Text = "0"
  162. DecreaseSizeBox3.Position = UDim2.new((2/3),0,0,0)
  163. DecreaseSizeBox3.TextSize = 13
  164. DecreaseSizeBox3.ClearTextOnFocus = false
  165. DecreaseSizeBox3.BackgroundTransparency = 1
  166. DecreaseSizeBox3.Parent = DecreaseSizeFrame
  167.  
  168.  
  169. local MiniOutput = Instance.new("TextLabel")
  170. MiniOutput.Text = ""
  171. MiniOutput.Size = UDim2.new(1,0,0.1,0)
  172. MiniOutput.Position = UDim2.new(0,0,0.9,0)
  173. MiniOutput.BorderSizePixel = 0
  174. MiniOutput.TextScaled = true
  175. table.insert(LeftSide, MiniOutput)
  176.  
  177. local MaterialBtn = Instance.new("TextButton")
  178. MaterialBtn.Text = "Set material"
  179. MaterialBtn.BorderSizePixel = 0
  180. MaterialBtn.TextSize = 13
  181. table.insert(LeftSide,MaterialBtn)
  182. MaterialBtn.LayoutOrder = Order
  183. Order = Order + 1
  184.  
  185. local MaterialBox = Instance.new("TextBox")
  186. MaterialBox.Text = "Plastic"
  187. MaterialBox.BorderSizePixel = 0
  188. MaterialBox.ClearTextOnFocus = false
  189. MaterialBox.TextSize = 13
  190. table.insert(RightSide,MaterialBox)
  191. MaterialBox.LayoutOrder = Order
  192. Order = Order + 1
  193.  
  194. local NameBtn = Instance.new("TextButton")
  195. NameBtn.Text = "Set name"
  196. NameBtn.BorderSizePixel = 0
  197. NameBtn.TextSize = 13
  198. table.insert(LeftSide,NameBtn)
  199. NameBtn.LayoutOrder = Order
  200. Order = Order + 1
  201.  
  202. local NameBtnBox = Instance.new("TextBox")
  203. NameBtnBox.Text = "Part"
  204. NameBtnBox.BorderSizePixel = 0
  205. NameBtnBox.TextSize = 13
  206. NameBtnBox.ClearTextOnFocus = false
  207. table.insert(RightSide,NameBtnBox)
  208. NameBtnBox.LayoutOrder = Order
  209. Order = Order + 1
  210.  
  211. local DefaultBtn = Instance.new("TextButton")
  212. DefaultBtn.Text = "Reset default settings"
  213. DefaultBtn.BorderSizePixel = 0
  214. DefaultBtn.TextSize = 13
  215. table.insert(LeftSide,DefaultBtn)
  216. DefaultBtn.LayoutOrder = Order
  217. Order = Order + 1
  218.  
  219. local Help = Instance.new("TextButton")
  220. Help.Text = "Help"
  221. Help.BorderSizePixel = 0
  222. Help.TextSize = 13
  223. table.insert(RightSide,Help)
  224. Help.LayoutOrder = Order
  225. Order = Order + 1
  226.  
  227. MainFrame.Parent = SettingsWidget
  228. PartTypeBtn.Parent = MainFrame
  229. PartTypeBox.Parent = MainFrame
  230. PrintSupportedTypes.Parent = MainFrame
  231. FitBall.Parent = MainFrame
  232. IncrementSize.Parent = MainFrame
  233. IncrementSizeFrame.Parent = MainFrame
  234. DecreaseSize.Parent = MainFrame
  235. DecreaseSizeFrame.Parent = MainFrame
  236. MiniOutput.Parent = SettingsWidget
  237. DefaultBtn.Parent = MainFrame
  238. Help.Parent = MainFrame
  239. NameBtn.Parent = MainFrame
  240. NameBtnBox.Parent = MainFrame
  241. MaterialBox.Parent = MainFrame
  242. MaterialBtn.Parent = MainFrame
  243.  
  244.  
  245. -- Ui object environment colours management
  246.  
  247. for i, v in ipairs(LeftSide) do
  248.     if v:IsA("TextButton") or v:IsA("TextLabel") or v:IsA("TextBox") or v:IsA("Frame") then
  249.         v.BackgroundTransparency = 0.75
  250.         v.BackgroundColor3 = Color3.fromRGB(157, 157, 157)
  251.         if v:IsA("TextButton") or v:IsA("TextLabel") or v:IsA("TextBox") and v~= MiniOutput then
  252.             v.TextScaled = false
  253.             v.TextSize = 13
  254.             v.TextTruncate = Enum.TextTruncate.AtEnd
  255.         end
  256.     end
  257. end
  258.  
  259. for i, v in pairs(RightSide) do
  260.     if v:IsA("TextButton") or v:IsA("TextLabel") or v:IsA("TextBox") or v:IsA("Frame") then
  261.         v.BackgroundTransparency = 0.75
  262.         v.BackgroundColor3 = Color3.fromRGB(208, 208, 208)
  263.         if v:IsA("TextButton") or v:IsA("TextLabel") or v:IsA("TextBox") and v ~= MiniOutput then
  264.             v.TextScaled = false
  265.             v.TextSize = 13
  266.             v.TextTruncate = Enum.TextTruncate.AtEnd
  267.         end
  268.     end
  269. end
  270.  
  271. MainFrame.BackgroundColor3 = settings().Studio.Theme:GetColor(Enum.StudioStyleGuideColor.MainBackground)
  272. MainFrame.BackgroundTransparency = 0
  273.  
  274.  
  275.  
  276. -- Events/functions
  277.  
  278.  
  279. MainButton.Click:Connect(function()
  280.     local Selection = game:GetService("Selection") -- Service required for getting selected objetcs
  281.    
  282.     -- if player selected at least one thing
  283.     if #Selection:Get() >= 1 then
  284.         -- Loop through selection
  285.         for i,v in pairs(Selection:Get()) do
  286.             if v:IsA("Model") == true then
  287.                
  288.                 local NewPart = Instance.new("Part")
  289.                 -- Get model CFrame and size
  290.                 local cframe, size = v:GetBoundingBox()
  291.                
  292.                 -- Checks if part can be party type enum
  293.                 local Succes, Response = pcall(function()
  294.                     local Temp = Enum.PartType[PartType]
  295.                 end)
  296.                 --
  297.                
  298.                 if Succes == false then
  299.                     if PartType == "MeshPart" then
  300.                         NewPart = Instance.new("MeshPart")
  301.                     elseif PartType == "Wedge" then
  302.                         NewPart = Instance.new("WedgePart")
  303.                     elseif PartType == "WedgePart" then
  304.                         NewPart = Instance.new("WedgePart")
  305.                     elseif PartType == "SpawnLocation" then
  306.                         NewPart = Instance.new("SpawnLocation")
  307.                     else
  308.                         warn("---------\nList of part types\n"..(PartTypeStrings))
  309.                     end
  310.                 else
  311.                     NewPart = Instance.new("Part")
  312.                     NewPart.Shape = Enum.PartType[PartType]
  313.                 end
  314.                
  315.                 -- Create new part
  316.                
  317.                 NewPart.CFrame = cframe
  318.                 if PartType == "Ball" and FitBall == true then
  319.                     local FinalVec = size
  320.                     local low = math.huge
  321.                     local index
  322.                     local Vec = {
  323.                         x = size.x,
  324.                         y = size.y,
  325.                         z = size.z
  326.                     }
  327.                     for i, v in pairs(Vec) do
  328.                         if v < low then
  329.                             low = v
  330.                         end
  331.                     end
  332.                    
  333.                    
  334.                     NewPart.Size = Vector3.new(low,low,low)
  335.                    
  336.                 else
  337.                     NewPart.Size = size
  338.                 end
  339.                
  340.                 NewPart.Material = PartMaterial
  341.                 NewPart.Size = NewPart.Size + SizeIncrement
  342.                 NewPart.Size = NewPart.Size - SizeDecrease
  343.                 NewPart.Name = PartName
  344.                 NewPart.FrontSurface = Enum.SurfaceType.Smooth
  345.                 NewPart.BackSurface = Enum.SurfaceType.Smooth
  346.                 NewPart.BottomSurface = Enum.SurfaceType.Smooth
  347.                 NewPart.LeftSurface = Enum.SurfaceType.Smooth
  348.                 NewPart.RightSurface = Enum.SurfaceType.Smooth
  349.                 NewPart.TopSurface = Enum.SurfaceType.Smooth
  350.                 NewPart.Parent = v
  351.                 Selection:Set({NewPart})
  352.                 -- Allow undoing
  353.                 ChangeHistoryService:SetWaypoint("Create part")
  354.             end
  355.         end
  356.     end
  357. end)
  358.  
  359. SettingsButton.Click:Connect(function()
  360.     if SettingsWidget.Enabled == true then
  361.         SettingsWidget.Enabled = false
  362.     else
  363.         SettingsWidget.Enabled = true
  364.     end
  365. end)
  366.  
  367. PartTypeBtn.MouseButton1Click:Connect(function()
  368.     local Text = PartTypeBox.Text
  369.     local Succes, Response = pcall(function()
  370.         local Temp = Enum.PartType[Text]
  371.     end)
  372.    
  373.     if Succes == false then
  374.         if Text == "MeshPart" then
  375.             PartType = "MeshPart"
  376.             MiniOutput.Text = "Set part type to MeshPart"
  377.         elseif Text == "Wedge" then
  378.             PartType = "Wedge"
  379.             MiniOutput.Text = "Set part type to Wedge"
  380.         elseif Text == "WedgePart" then
  381.             PartType = "WedgePart"
  382.             MiniOutput.Text = "Set part type to Wedge"
  383.         elseif Text == "SpawnLocation" then
  384.             PartType = "SpawnLocation"
  385.             MiniOutput.Text = "Set part type to Spawn location"
  386.         else
  387.             warn("---------\nList of part types\n"..(PartTypeStrings))
  388.             PartTypeBox.Text = "Block"
  389.             MiniOutput.Text = "See output for supported types"
  390.         end
  391.     else
  392.         PartType = Text
  393.         MiniOutput.Text = "Set part type to "..Text
  394.     end
  395.    
  396. end)
  397.  
  398. PrintSupportedTypes.MouseButton1Click:Connect(function()
  399.     warn("---------\nList of part types\n"..(PartTypeStrings))
  400.     MiniOutput.Text = "See output for supported types"
  401. end)
  402.  
  403. FitBall.MouseButton1Click:Connect(function()
  404.     if FitBall == true then
  405.         FitBall = false
  406.         MiniOutput.Text = "Set fit ball to false"
  407.     else
  408.         FitBall = true
  409.         MiniOutput.Text = "Set fit ball to true"
  410.     end
  411. end)
  412.  
  413. local IncrementDebounce = false
  414. IncrementSize.MouseButton1Click:Connect(function()
  415.     if IncrementDebounce == false then
  416.         IncrementDebounce = true
  417.         local X
  418.         local Y
  419.         local Z
  420.         if tonumber(IncrementSizeBox1.Text) ~= nil then
  421.             X = math.abs(tonumber(IncrementSizeBox1.Text))
  422.             if X > 1000 then
  423.                 X = 1000
  424.             end
  425.         else
  426.             X = 0
  427.         end
  428.         if tonumber(IncrementSizeBox2.Text) ~= nil then
  429.             Y = math.abs(tonumber(IncrementSizeBox2.Text))
  430.             if Y > 1000 then
  431.                 Y = 1000
  432.             end
  433.         else
  434.             Y = 0
  435.         end
  436.         if tonumber(IncrementSizeBox3.Text) ~= nil then
  437.             Z = math.abs(tonumber(IncrementSizeBox3.Text))
  438.             if Z > 1000 then
  439.                 Z = 1000
  440.             end
  441.         else
  442.             Z = 0
  443.         end
  444.         SizeIncrement = Vector3.new(X,Y,Z)
  445.         MiniOutput.Text = "Set increment of: "..(tostring(SizeIncrement))
  446.         IncrementDebounce = false
  447.     end
  448. end)
  449.  
  450. local DecreaseDebounce = false
  451. DecreaseSize.MouseButton1Click:Connect(function()
  452.     if DecreaseDebounce == false then
  453.         DecreaseDebounce = true
  454.         local X
  455.         local Y
  456.         local Z
  457.         if tonumber(DecreaseSizeBox1.Text) ~= nil then
  458.             X = math.abs(tonumber(DecreaseSizeBox1.Text))
  459.             if X > 1000 then
  460.                 X = 1000
  461.             end
  462.         else
  463.             X = 0
  464.         end
  465.         if tonumber(DecreaseSizeBox2.Text) ~= nil then
  466.             Y = math.abs(tonumber(DecreaseSizeBox2.Text))
  467.             if Y > 1000 then
  468.                 Y = 1000
  469.             end
  470.         else
  471.             Y = 0
  472.         end
  473.         if tonumber(DecreaseSizeBox3.Text) ~= nil then
  474.             Z = math.abs(tonumber(DecreaseSizeBox3.Text))
  475.             if Z > 1000 then
  476.                 Z = 1000
  477.             end
  478.         else
  479.             Z = 0
  480.         end
  481.         SizeDecrease = Vector3.new(X,Y,Z)
  482.         MiniOutput.Text = "Set decrease of: "..(tostring(SizeDecrease))
  483.         DecreaseDebounce = false
  484.     end
  485. end)
  486.  
  487. DefaultBtn.MouseButton1Click:Connect(function()
  488.     PartType = "Block"
  489.     FitBall = false
  490.     SizeIncrement = Vector3.new(0,0,0)
  491.     SizeDecrease = Vector3.new(0,0,0)
  492.     NameBtnBox.Text = "Part"
  493.     MaterialBox.Text = "Plastic"
  494.     PartTypeBox.Text = "Block"
  495.     IncrementSizeBox1.Text = "0"
  496.     IncrementSizeBox2.Text = "0"
  497.     IncrementSizeBox3.Text = "0"
  498.     DecreaseSizeBox1.Text = "0"
  499.     DecreaseSizeBox2.Text = "0"
  500.     DecreaseSizeBox3.Text = "0"
  501.     PartMaterial = Enum.Material.Plastic
  502.     PartName = "Part"
  503.     MiniOutput.Text = "Set default settings"
  504. end)
  505.  
  506. Help.MouseButton1Click:Connect(function()
  507.     warn(HelpMessage)
  508.     MiniOutput.Text = "See output for details"
  509. end)
  510.  
  511. MaterialBtn.MouseButton1Click:Connect(function()
  512.     local Succes, Message = pcall(function()
  513.         local Temp = Enum.Material[MaterialBox.Text]
  514.     end)
  515.     if Succes then
  516.         PartMaterial = Enum.Material[MaterialBox.Text]
  517.         MiniOutput.Text = "Set material to "..MaterialBox.Text
  518.     else
  519.         MaterialBox.Text = "Plastic"
  520.         PartMaterial = Enum.Material.Plastic
  521.         MiniOutput.Text = "Set material to Plastic"
  522.     end
  523. end)
  524.  
  525. NameBtn.MouseButton1Click:Connect(function()
  526.     PartName = NameBtnBox.Text
  527.     MiniOutput.Text = ("Set name to "..PartName)
  528. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement