Here’s all the code
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local InventoryStore = DataStoreService:GetDataStore("InventoryStore")
local StackLimits = require(workspace.StackLimits)
local Inventory = require(script.Inventory)
local InventoryTables = {}
local function CreateNewInventory(Player)
local ID = Player.UserId
local inventory = Inventory.new(ID)
InventoryTables[ID] = inventory
end
local function SaveInventory(Player)
local PlayerInventory = InventoryTables[Player.UserId]
local InventoryInfo = HttpService:JSONEncode(PlayerInventory)
print(InventoryInfo, " This is what the inventory looks like when being saved")
InventoryStore:SetAsync(Player.UserId,InventoryInfo)
end
local function LoadInventory(Player)
local Data = InventoryStore:GetAsync(Player.UserId)
local PlayerInventory
if Data then
local InventoryData = HttpService:JSONDecode(Data)
print(InventoryData,"This is what's being pulled when JSONDecoding")
PlayerInventory = Inventory.new(Player.UserId)
PlayerInventory:LoadFromData(InventoryData)
print(PlayerInventory, " This is what the inventory looks like when being loaded")
else
PlayerInventory = Inventory.new(Player.UserId)
end
InventoryTables[Player.UserId] = PlayerInventory
end
local function GetInventory(Player)
local PlayerInventory = InventoryTables[Player.UserId]
print(PlayerInventory, "This is the inventory when mapping")
game.ReplicatedStorage.Inventory.MapInventory:FireClient(Player, PlayerInventory)
end
local function SubtractItem(Player,args)
print(Player,args)
local Inventory = InventoryTables[Player.UserId]
local i = 1
while true do
if args["Item"..i] then
local Subtract = Inventory:SubtractItem(args,i)
i+=1
else
break
end
end
end
game.ReplicatedStorage.Inventory.AddItemBindable.Event:Connect(function(player,args)
print(args)
local PlayerInventory = InventoryTables[args.PlayerID]
local i = 1
while true do
if args["Item"..i] then
local AddItem = PlayerInventory:AddItem(args,i)
if AddItem then
PlayerInventory:StackFixer(args,i)
end
local FullFixer = PlayerInventory:FullFixer(args,i)
if FullFixer then
args["Item"..i] = nil
end
i+=1
else
args.Location.Finished:Fire(args)
i = 1
break
end
end
warn(Inventory)
end)
--game.Players.PlayerAdded:Connect(CreateNewInventory)
game.Players.PlayerAdded:Connect(LoadInventory)
game.Players.PlayerRemoving:Connect(SaveInventory)
game.ReplicatedStorage.Inventory.MapInventory.OnServerEvent:Connect(GetInventory)
game.ReplicatedStorage.Inventory.SubtractItem.OnServerEvent:Connect(SubtractItem)
local StackLimits = require(workspace.StackLimits)
local Inventory = {}
local Item
local Amount
local CompletedAdd = false
local MAX_SLOTS = 28
local CurrentSlot
Inventory.__index = Inventory
function Inventory.new(player)
local i = 1
local self = {}
self.Player = player
self.Data = {}
for i = 1, MAX_SLOTS do
self.Data[i] = {}
end
self.Data["Extra"] = {}
i = 1
return setmetatable(self, Inventory)
end
function Inventory:NewSlot(Data,NewItem,NewAmount,TotalAmount)
for Key,Slot in pairs(Data) do
if next(Slot) == nil then
if Key == "Extra" then
Data["Extra"][NewItem] = TotalAmount
return false
else
CurrentSlot = Key
Data[Key][NewItem] = NewAmount
return true
end
end
if not (Key == "Extra") then
Key += 1
else
break
end
end
end
function Inventory:AddItem(args,i)
local Location = args.Location
CompletedAdd = false
local Passed = false
local Extra
if args["Item"..i] then
Item = args["Item"..i]
Amount = args["Num"..i]
for Key,Slot in self.Data do
print(self.Data,Slot,Slot[Item])
if Slot[Item] and Slot[Item] < StackLimits[Item] then
Passed = true
Slot[Item] += Amount
CurrentSlot = Key
if Slot[Item] > StackLimits[Item] then
warn("Slot went over the stack limit whilst adding onto an existing slot")
Extra = Slot[Item] - StackLimits[Item]
CurrentSlot = Key
end
return true
elseif not Slot[Item] then
Passed = false
end
end
print(Passed)
if Passed == false then
print("New slot is being created for",Item)
local NewSlot = Inventory:NewSlot(self.Data,Item,Amount)
print(self.Data)
if NewSlot then
return true
else
return false
end
end
i+=1
end
end
function Inventory:SubtractItem(args,i)
if args["Item"..i] then
Item = args["Item"..i]
Amount = args["Num"..i]
for Key,Slot in pairs(self.Data) do
warn(Item)
if Slot[Item] then
warn(Slot[Item])
Slot[Item] -= Amount
if Slot[Item] <= 0 then
self.Data[Key]={}
print(self.Data)
end
end
end
end
end
function Inventory:StackFixer(args,i)
if CompletedAdd == false then
if self.Data[CurrentSlot][Item] > StackLimits[Item] then
warn("Our current item is set to",self.Data[CurrentSlot][Item])
local FullStacks = math.floor(self.Data[CurrentSlot][Item]/StackLimits[Item])
local Remaning = self.Data[CurrentSlot][Item] % StackLimits[Item]
local TotalAmount = (FullStacks*StackLimits[Item])+Remaning
warn(FullStacks,Remaning)
self.Data[CurrentSlot][Item] = StackLimits[Item]
while FullStacks > 1 do
Item = args["Item"..i]
Amount = StackLimits[Item]
local NewSlot = Inventory:NewSlot(self.Data,Item,Amount,TotalAmount)
if NewSlot then
FullStacks -= 1
TotalAmount -= Amount
print(TotalAmount)
else
FullStacks = 0
end
end
if Remaning > 0 then
Item = args["Item"..i]
Amount = Remaning
Inventory:NewSlot(self.Data,Item,Amount,TotalAmount)
end
end
else
return
end
end
function Inventory:FullFixer(args,i)
if CompletedAdd == false then
if next(self.Data["Extra"]) == nil then
return true
else
local Location = args.Location
local ItemSurplus = self.Data["Extra"][Item]
warn("GetBack Firing")
Location:FindFirstChild("GetBack"):Fire(ItemSurplus,i)
self.Data["Extra"] = {}
end
else
return
end
end
function Inventory:LoadFromData(data)
warn(data)
self = data
warn(self)
return self
end
return Inventory
Another thing that might be important is that the variable InventoryInfo prints
" {“Data”:[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]],“Player”:1572007720}"
instead of one of the {…}