Advertisement
Guest User

Inventory Placement

a guest
Aug 10th, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. local player = game.Players.LocalPlayer
  2. local mouse = player:GetMouse()
  3.  
  4. local gui = script.Parent
  5. local frameMain = gui:WaitForChild("Frame")
  6.  
  7. local amount = 12 -- total = amount^2
  8.  
  9. for y = 1, amount do
  10.     for x = 1, amount do
  11.         local newFrame = Instance.new("Frame")
  12.         newFrame.ZIndex = 2
  13.         newFrame.Size = UDim2.new(0, frameMain.AbsoluteSize.X / amount, 0, frameMain.AbsoluteSize.Y / amount)
  14.         newFrame.Position = UDim2.new(0, (x - 1) * frameMain.AbsoluteSize.X / amount, 0, (y - 1) * frameMain.AbsoluteSize.Y / amount)
  15.         newFrame.Parent = frameMain
  16.     end
  17. end
  18.  
  19. local dragging = false
  20.  
  21. local thing = Instance.new("Frame")
  22. thing.ZIndex = 3
  23. thing.Size = UDim2.new(0, frameMain.AbsoluteSize.X / amount, 0, frameMain.AbsoluteSize.Y / amount)
  24. thing.Position = UDim2.new(0.5, -thing.Size.X.Offset / 2, 0, 0)
  25. thing.BackgroundColor3 = Color3.new(0, 1, 0)
  26. thing.InputBegan:Connect(function(input)
  27.     if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
  28.         return
  29.     end
  30.    
  31.     dragging = true
  32. end)
  33. thing.InputEnded:Connect(function(input)
  34.     if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
  35.         return
  36.     end
  37.    
  38.     dragging = false
  39.    
  40.     local frames = frameMain:GetChildren()
  41.     do
  42.         local mousePos = Vector2.new(input.Position.X, input.Position.Y)
  43.         table.sort(frames, function(f1, f2)
  44.             local f1Pos = f1.AbsolutePosition + f1.AbsoluteSize / 2
  45.             local f2Pos = f2.AbsolutePosition + f2.AbsoluteSize / 2
  46.            
  47.             return (mousePos - f1Pos).Magnitude < (mousePos - f2Pos).Magnitude
  48.         end)
  49.     end
  50.    
  51.     thing.Position = UDim2.new(0, frames[1].AbsolutePosition.X, 0, frames[1].AbsolutePosition.Y)
  52. end)
  53.  
  54. game:GetService("RunService").RenderStepped:Connect(function()
  55.     if dragging then
  56.         thing.Position = UDim2.new(0, mouse.X - thing.Size.X.Offset / 2, 0, mouse.Y - thing.Size.Y.Offset / 2)
  57.     end
  58. end)
  59.  
  60. thing.Parent = gui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement