Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local ServerScriptService = game:GetService("ServerScriptService")
- local ContainerUtil = require(ReplicatedStorage.Shared.Utils.ContainerUtil)
- local DataManagerShared = require(ReplicatedStorage.Shared.Modules.DataManagerShared)
- local InventoryManagerShared = require(ReplicatedStorage.Shared.Modules.InventoryManagerShared)
- local InventoryManager = {}
- function InventoryManager.InsertItem(playerContainer, itemData, amountToGive)
- if type(itemData) == "string" then
- itemData = InventoryManagerShared.Items[itemData]
- end
- local amountLeft = amountToGive
- while amountLeft > 0 do
- local itemSizeCount = 0
- for _, otherItemSlot in playerContainer:GetValue("Items") do
- local otherItemData = InventoryManagerShared.Items[otherItemSlot.Key]
- if not otherItemData then
- continue
- end
- if otherItemData.Size == itemData.Size then
- itemSizeCount += otherItemSlot.Amount
- end
- end
- if itemSizeCount >= InventoryManagerShared.Sizes[itemData.Size] then
- break
- end
- local itemSlot
- local itemSlotIndex
- for i, otherItemSlot in playerContainer:GetValue("Items") do
- local otherItemData = InventoryManagerShared.Items[otherItemSlot.Key]
- if not otherItemData then
- continue
- end
- if otherItemData.Size ~= itemData.Size or
- otherItemSlot.Key ~= itemData.Key
- then
- continue
- end
- if itemData.Stack and otherItemSlot.Amount >= itemData.Stack then
- continue
- end
- itemSlot = otherItemSlot
- itemSlotIndex = i
- end
- if not itemSlot then
- itemSlotIndex = #playerContainer:GetValue("Items") + 1
- itemSlot = {
- Key = itemData.Key,
- Amount = 0,
- }
- ContainerUtil.InsertToTable(playerContainer, {"Items"}, itemSlot)
- end
- local stackSize = itemData.Stack or InventoryManagerShared.Sizes[itemData.Size]
- local amountToAdd = math.min(stackSize - itemSlot.Amount, amountLeft)
- amountLeft -= amountToAdd
- playerContainer:SetValue({"Items", itemSlotIndex, "Amount"}, itemSlot.Amount + amountToAdd)
- end
- return amountToGive - amountLeft
- end
- DataManagerShared.PlayerContainerHolder.ContainerAdded:Connect(function(player, playerContainer)
- while true do
- task.wait(3)
- local index = math.random(1, #InventoryManagerShared.Items)
- local key = InventoryManagerShared.Items:GetKeyByIndex(index)
- local data = InventoryManagerShared.Items[key]
- InventoryManager.InsertItem(playerContainer, data, 5)
- print(playerContainer)
- end
- end)
- return InventoryManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement