Advertisement
nyrhtghyjeawpp

Ledge Creator

Dec 10th, 2023 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.06 KB | None | 0 0
  1. local toolbar = plugin:CreateToolbar("Ledge Creator")
  2.  
  3. local uis = game:GetService("UserInputService")
  4. local SelectionService = game:GetService("Selection")
  5. local studio_service = game:GetService("StudioService")
  6.  
  7.  
  8. local pluginButton = toolbar:CreateButton(
  9.     "call",
  10.     "Create ledge for selected parts",
  11.     "rbxassetid://2782179754"
  12. )
  13.  
  14.  
  15.  
  16. local ledge_SIZE_LIMIT = NumberRange.new(0.01, 100)
  17. local ledge_HEIGHT_LIMIT = NumberRange.new(0.01, 100)
  18.  
  19.  
  20.  
  21.  
  22. local ledge_parts: {BasePart} = {}
  23. local mouse_start_pos: Vector3 = Vector3.zero
  24. local mouse_pos: Vector3 = Vector3.zero
  25. local mouse1_down: boolean = false
  26.  
  27. local ledge_heighting: boolean = false
  28. local ledge_sizing: boolean = false
  29.  
  30. local invert_height: boolean = false
  31.  
  32.  
  33. local DEFAULT_SIZE: number = 0.1
  34. local DEFAULT_HEIGHT: number = 0.2
  35.  
  36.  
  37.  
  38.  
  39. plugin:Activate(false)
  40. task.wait()
  41. local mouse: PluginMouse = plugin:GetMouse()
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. pluginButton.Click:Connect(function()
  53.     mouse_start_pos = mouse_pos
  54.  
  55.     local selection = game:GetService('Selection'):Get()
  56.     if not selection[1] then print('ledge creator -- empty selection')return end
  57.  
  58.  
  59.  
  60.     table.clear(ledge_parts)
  61.  
  62.  
  63.  
  64.  
  65.     for _, v in selection do
  66.         if v:IsA('BasePart')then
  67.             local new_ledge = v:FindFirstChild('LedgeC')or v:Clone()
  68.  
  69.  
  70.  
  71.             new_ledge.Name = 'LedgeC'
  72.             new_ledge.Parent = v.Parent or workspace
  73.  
  74.             for _, v in Enum.NormalId:GetEnumItems()do
  75.                 local new_decal = Instance.new('Decal', new_ledge)
  76.                 new_decal.Name = 'ledge_preview'
  77.                 new_decal.Texture = 'rbxassetid://15396807460'
  78.  
  79.                 new_decal.Transparency = 0.5
  80.                 new_decal.Face = v
  81.             end
  82.  
  83.             ledge_parts[new_ledge] = v
  84.         end
  85.     end
  86.  
  87.  
  88.  
  89.     invert_height = false
  90.     ledge_heighting = true
  91. end)
  92.  
  93.  
  94.  
  95.  
  96. local function clear_decals(v: BasePart): nil
  97.     for _, decal: Decal in v:GetChildren()do
  98.         if decal.Name == 'ledge_preview'then
  99.             decal:Destroy()
  100.         end
  101.     end
  102. end
  103.  
  104.  
  105.  
  106.  
  107. uis.InputBegan:Connect(function(input: InputObject)
  108.     if ledge_heighting or ledge_sizing then
  109.         if input.KeyCode.Name == 'Escape' then
  110.             ledge_heighting = false
  111.             ledge_sizing = false
  112.  
  113.             for v: BasePart,_ in ledge_parts do
  114.                 v:Destroy()
  115.             end
  116.  
  117.             table.clear(ledge_parts)
  118.             return
  119.         end
  120.  
  121.  
  122.  
  123.         if input.UserInputType.Name == 'MouseButton1' then
  124.             if ledge_heighting then
  125.                 ledge_heighting = false
  126.                 ledge_sizing = true
  127.                 mouse_start_pos = mouse_pos
  128.             else
  129.                 ledge_heighting = false
  130.                 ledge_sizing = false
  131.  
  132.                 local parts: {BasePart} = {}
  133.                 for v: BasePart,_ in ledge_parts do
  134.                     clear_decals(v)
  135.                     table.insert(parts, v)
  136.                 end
  137.                 game:GetService('Selection'):Set(parts)
  138.  
  139.                 table.clear(ledge_parts)
  140.             end
  141.         elseif input.UserInputType.Name == 'MouseButton3' then
  142.             invert_height = not invert_height
  143.         end
  144.     end
  145. end)
  146.  
  147.  
  148.  
  149.  
  150. local function snap_number(n: number): number
  151.     return math.floor((n / plugin.GridSize)) * plugin.GridSize
  152. end
  153.  
  154.  
  155. game:GetService('RunService').Heartbeat:Connect(function(dt)
  156.     local position = Vector2.new(mouse.X, mouse.Y)
  157.  
  158.     mouse_pos = position
  159.  
  160.  
  161.  
  162.     if ledge_heighting or ledge_sizing then
  163.         local max: number = ((ledge_heighting and ledge_HEIGHT_LIMIT.Max)or ledge_SIZE_LIMIT.Max)
  164.         local mouse_sens: number = (ledge_heighting and 0.5) or 0.05
  165.  
  166.  
  167.         local value: number = (position.X - mouse_start_pos.X) * mouse_sens
  168.         local factor: number = math.clamp((position.X - mouse_start_pos.X) * mouse_sens, 0.01, max)
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.         if ledge_parts ~= nil then
  176.             for v: BasePart, parent: BasePart in ledge_parts do
  177.                 if ledge_sizing then
  178.                     v.Size = Vector3.new(snap_number(parent.Size.X + math.max(factor, ledge_SIZE_LIMIT.Min)),   v.Size.Y,   snap_number(parent.Size.Z + math.max(factor, ledge_SIZE_LIMIT.Min)))
  179.                 else
  180.                     v.Size = Vector3.new(parent.Size.X + 1,   snap_number(parent.Size.Y * (math.clamp((factor / max), 0.01, 0.99))),   parent.Size.Z + 1)
  181.                    
  182.                     local y = -parent.Size.Y / 2.0 + (v.Size.Y / 2.0) - 0.005
  183.                     if invert_height then
  184.                         y = parent.Size.Y / 2.0 - (v.Size.Y / 2.0) + 0.005
  185.                     end
  186.                     v.CFrame = parent.CFrame * CFrame.new(0,   y,   0)
  187.                 end
  188.             end
  189.         end
  190.     end
  191. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement