Advertisement
AlewAlow

inventory

May 2nd, 2024 (edited)
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local ServerScriptService = game:GetService("ServerScriptService")
  4.  
  5. local ContainerUtil = require(ReplicatedStorage.Shared.Utils.ContainerUtil)
  6. local DataManagerShared = require(ReplicatedStorage.Shared.Modules.DataManagerShared)
  7. local InventoryManagerShared = require(ReplicatedStorage.Shared.Modules.InventoryManagerShared)
  8. local InventoryManager = {}
  9.  
  10. function InventoryManager.InsertItem(playerContainer, itemData, amountToGive)
  11.     if type(itemData) == "string" then
  12.         itemData = InventoryManagerShared.Items[itemData]
  13.     end
  14.    
  15.     local amountLeft = amountToGive
  16.     while amountLeft > 0 do
  17.         local itemSizeCount = 0
  18.        
  19.         for _, otherItemSlot in playerContainer:GetValue("Items") do
  20.             local otherItemData = InventoryManagerShared.Items[otherItemSlot.Key]
  21.             if not otherItemData then
  22.                 continue
  23.             end
  24.            
  25.             if otherItemData.Size == itemData.Size then
  26.                 itemSizeCount += otherItemSlot.Amount
  27.             end
  28.         end
  29.        
  30.         if itemSizeCount >= InventoryManagerShared.Sizes[itemData.Size] then
  31.             break
  32.         end
  33.        
  34.         local itemSlot
  35.         local itemSlotIndex
  36.  
  37.         for i, otherItemSlot in playerContainer:GetValue("Items") do
  38.             local otherItemData = InventoryManagerShared.Items[otherItemSlot.Key]
  39.             if not otherItemData then
  40.                 continue
  41.             end
  42.  
  43.             if otherItemData.Size ~= itemData.Size or
  44.                 otherItemSlot.Key ~= itemData.Key
  45.             then
  46.                 continue
  47.             end
  48.            
  49.             if itemData.Stack and otherItemSlot.Amount >= itemData.Stack then
  50.                 continue
  51.             end
  52.  
  53.             itemSlot = otherItemSlot
  54.             itemSlotIndex = i
  55.         end
  56.        
  57.         if not itemSlot then
  58.             itemSlotIndex = #playerContainer:GetValue("Items") + 1
  59.             itemSlot = {
  60.                 Key = itemData.Key,
  61.                 Amount = 0,
  62.             }
  63.  
  64.             ContainerUtil.InsertToTable(playerContainer, {"Items"}, itemSlot)
  65.         end
  66.        
  67.         local stackSize = itemData.Stack or InventoryManagerShared.Sizes[itemData.Size]
  68.         local amountToAdd = math.min(stackSize - itemSlot.Amount, amountLeft)
  69.         amountLeft -= amountToAdd
  70.         playerContainer:SetValue({"Items", itemSlotIndex, "Amount"}, itemSlot.Amount + amountToAdd)
  71.     end
  72.    
  73.     return amountToGive - amountLeft
  74. end
  75.  
  76. DataManagerShared.PlayerContainerHolder.ContainerAdded:Connect(function(player, playerContainer)
  77.     while true do
  78.         task.wait(3)
  79.        
  80.         local index = math.random(1, #InventoryManagerShared.Items)
  81.         local key = InventoryManagerShared.Items:GetKeyByIndex(index)
  82.         local data = InventoryManagerShared.Items[key]
  83.        
  84.         InventoryManager.InsertItem(playerContainer, data, 5)
  85.         print(playerContainer)
  86.     end
  87. end)
  88.  
  89. return InventoryManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement