Advertisement
TheQP

Inventory System Script

Jul 19th, 2024 (edited)
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | Source Code | 0 0
  1. -- Inventory System Script
  2. - Place script in ScreenGui
  3.  
  4. -[[
  5. ScreenGui
  6.     InventoryFrame (Frame)
  7.     ItemTemplate (Button)
  8. ]]
  9.  
  10. local player = game.Players.LocalPlayer
  11. local playerGui = player:WaitForChild("PlayerGui")
  12. local inventoryFrame = playerGui:WaitForChild("ScreenGui"):WaitForChild("InventoryFrame")
  13. local itemTemplate = inventoryFrame:WaitForChild("ItemTemplate")
  14.  
  15. local maxInventorySize = 20
  16. local inventory = {}
  17.  
  18. -- Function to add an item to the inventory
  19. local function addItem(itemName, quantity)
  20.     for _, item in ipairs(inventory) do
  21.         if item.name == itemName then
  22.             item.quantity = item.quantity + quantity
  23.             item.slot.Button.Text = item.name .. " (" .. item.quantity .. ")"
  24.             return
  25.         end
  26.     end
  27.  
  28.     if #inventory < maxInventorySize then
  29.         local newItem = {
  30.             name = itemName,
  31.             quantity = quantity,
  32.             slot = itemTemplate:Clone(),
  33.         }
  34.         newItem.slot.Parent = inventoryFrame
  35.         newItem.slot.Button.Text = newItem.name .. " (" .. newItem.quantity .. ")"
  36.         newItem.slot.Visible = true
  37.  
  38.         table.insert(inventory, newItem)
  39.         updateInventoryLayout()
  40.     else
  41.         print("Inventory full!")
  42.     end
  43. end
  44.  
  45. -- Function to remove an item from the inventory
  46. local function removeItem(itemName, quantity)
  47.     for index, item in ipairs(inventory) do
  48.         if item.name == itemName then
  49.             item.quantity = item.quantity - quantity
  50.             if item.quantity <= 0 then
  51.                 item.slot:Destroy()
  52.                 table.remove(inventory, index)
  53.             else
  54.                 item.slot.Button.Text = item.name .. " (" .. item.quantity .. ")"
  55.             end
  56.             return
  57.         end
  58.     end
  59.     print("Item not found in inventory!")
  60. end
  61.  
  62. -- Function to update the inventory layout
  63. local function updateInventoryLayout()
  64.     local padding = 5
  65.     local slotSize = itemTemplate.Size
  66.     local cols = math.floor(inventoryFrame.AbsoluteSize.X / (slotSize.X.Offset + padding))
  67.  
  68.     for i, item in ipairs(inventory) do
  69.         local row = math.floor((i - 1) / cols)
  70.         local col = (i - 1) % cols
  71.         item.slot.Position = UDim2.new(0, col * (slotSize.X.Offset + padding), 0, row * (slotSize.Y.Offset + padding))
  72.     end
  73. end
  74.  
  75. -- Function to handle item usage
  76. local function useItem(itemName)
  77.     for _, item in ipairs(inventory) do
  78.         if item.name == itemName then
  79.             print("Using item:", itemName)
  80.             removeItem(itemName, 1)
  81.             return
  82.         end
  83.     end
  84.     print("Item not found in inventory!")
  85. end
  86.  
  87. -- Adding drag and drop functionality
  88. local function enableDragAndDrop()
  89.     for _, item in ipairs(inventory) do
  90.         local slot = item.slot
  91.         local dragging = false
  92.         local dragStart, startPos
  93.  
  94.         local function updateDrag(input)
  95.             local delta = input.Position - dragStart
  96.             slot.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
  97.         end
  98.  
  99.         slot.InputBegan:Connect(function(input)
  100.             if input.UserInputType == Enum.UserInputType.MouseButton1 then
  101.                 dragging = true
  102.                 dragStart = input.Position
  103.                 startPos = slot.Position
  104.                 input.Changed:Connect(function()
  105.                     if input.UserInputState == Enum.UserInputState.End then
  106.                         dragging = false
  107.                         updateInventoryLayout() -- Re-organize layout after drop
  108.                     end
  109.                 end)
  110.             end
  111.         end)
  112.  
  113.         slot.InputChanged:Connect(function(input)
  114.             if dragging and input.UserInputType == Enum.UserInputType.MouseMovement then
  115.                 updateDrag(input)
  116.             end
  117.         end)
  118.     end
  119. end
  120.  
  121. -- Example usage
  122. addItem("Health Potion", 5)
  123. addItem("Mana Potion", 3)
  124. useItem("Health Potion")
  125. enableDragAndDrop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement