Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local Quire = _G.Quire
- local DataStore2 = Quire("DataStore2")
- local Enums = Quire("Enums")
- local NetworkUtil = Quire("NetworkUtil")
- local ItemList = Quire("ItemList")
- local SessionData = Quire("SessionData")
- local repr = require(3148021300)
- local INVENTORY_KEY = Enums.DataKeys.InventoryKey
- local DEFAULT_INVENTORY = {
- Slots = {
- [1] = {
- IsHotSlot = true,
- Item = nil
- },
- [2] = {
- IsHotSlot = true,
- Item = nil
- },
- [3] = {
- Item = nil
- },
- [4] = {
- Item = nil
- },
- [5] = {
- Item = nil
- },
- [6] = {
- Item = nil
- },
- [7] = {
- Item = nil
- },
- [8] = {
- Item = nil
- },
- }
- }
- local InventoryService = {}
- local function loadPlayerInventory(player)
- local inventory = DataStore2(INVENTORY_KEY, player):GetTable(DEFAULT_INVENTORY)
- InventoryService:AddItem(player, inventory, 1, 15)
- print(repr(inventory, {pretty = true}))
- end
- function InventoryService:AddItem(player, inventory, ID, amount)
- amount = amount or 1
- local slots = inventory.Slots
- local itemInfo = ItemList:GetItemFromID(ID)
- local maxStackSize = itemInfo.MaxStackSize
- local isStackable = maxStackSize > 1
- -- adds amount to open stacks of same id
- if isStackable then
- for slotIndex, slotData in ipairs(slots) do
- if amount < 1 then break end
- local item = slotData.Item
- local isSameId = item and item.ID == ID
- local availableAmount = isSameId and maxStackSize - item.StackSize
- if availableAmount and (availableAmount > 0) then
- local amountToAdd = math.clamp(amount, 0, availableAmount)
- item.StackSize = item.StackSize + amountToAdd
- amount = amount - amountToAdd
- end
- end
- end
- -- adds items to empty slots
- for slotIndex, slotData in ipairs(slots) do
- if amount < 1 then break end
- if not slotData.Item then
- local amountToAdd = math.clamp(amount, 0, maxStackSize)
- local newItem = {InfoLink = itemInfo, ID = ID, StackSize = amountToAdd}
- slotData.Item = newItem
- amount = amount - amountToAdd
- end
- end
- return amount -- remainder
- end
- Players.PlayerAdded:Connect(loadPlayerInventory)
- return InventoryService
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement