Advertisement
Quoteory

InventoryService V1

Feb 10th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. local Players = game:GetService("Players")
  2. local Quire = _G.Quire
  3. local DataStore2 = Quire("DataStore2")
  4. local Enums = Quire("Enums")
  5. local NetworkUtil = Quire("NetworkUtil")
  6. local ItemList = Quire("ItemList")
  7. local SessionData = Quire("SessionData")
  8. local repr = require(3148021300)
  9.  
  10. local INVENTORY_KEY = Enums.DataKeys.InventoryKey
  11. local DEFAULT_INVENTORY = {
  12.     Slots = {
  13.         [1] = {
  14.             IsHotSlot = true,
  15.             Item = nil
  16.         },
  17.         [2] = {
  18.             IsHotSlot = true,
  19.             Item = nil
  20.         },
  21.         [3] = {
  22.             Item = nil
  23.         },
  24.         [4] = {
  25.             Item = nil
  26.         },
  27.         [5] = {
  28.             Item = nil
  29.         },
  30.         [6] = {
  31.             Item = nil
  32.         },
  33.         [7] = {
  34.             Item = nil
  35.         },
  36.         [8] = {
  37.             Item = nil
  38.         },
  39.     }
  40. }
  41.  
  42.  
  43.  
  44. local InventoryService = {}
  45.  
  46. local function loadPlayerInventory(player)
  47.     local inventory = DataStore2(INVENTORY_KEY, player):GetTable(DEFAULT_INVENTORY)
  48.     InventoryService:AddItem(player, inventory, 1, 15)
  49.     print(repr(inventory, {pretty = true}))
  50. end
  51.  
  52. function InventoryService:AddItem(player, inventory, ID, amount)
  53.     amount = amount or 1
  54.  
  55.     local slots = inventory.Slots
  56.     local itemInfo = ItemList:GetItemFromID(ID)
  57.     local maxStackSize = itemInfo.MaxStackSize
  58.     local isStackable = maxStackSize > 1
  59.  
  60.  
  61.     -- adds amount to open stacks of same id
  62.     if isStackable then
  63.         for slotIndex, slotData in ipairs(slots) do
  64.             if amount < 1 then break end
  65.             local item = slotData.Item
  66.             local isSameId = item and item.ID == ID
  67.             local availableAmount = isSameId and maxStackSize - item.StackSize
  68.  
  69.             if availableAmount and (availableAmount > 0) then
  70.                 local amountToAdd = math.clamp(amount, 0, availableAmount)
  71.                 item.StackSize = item.StackSize + amountToAdd
  72.                 amount = amount - amountToAdd
  73.             end
  74.         end
  75.     end
  76.  
  77.     -- adds items to empty slots
  78.     for slotIndex, slotData in ipairs(slots) do
  79.         if amount < 1 then break end
  80.         if not slotData.Item then
  81.             local amountToAdd = math.clamp(amount, 0, maxStackSize)
  82.             local newItem = {InfoLink = itemInfo, ID = ID, StackSize = amountToAdd}
  83.             slotData.Item = newItem
  84.             amount = amount - amountToAdd
  85.         end
  86.     end
  87.  
  88.     return amount -- remainder
  89. end
  90.  
  91. Players.PlayerAdded:Connect(loadPlayerInventory)
  92.  
  93. return InventoryService
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement