Custom Inventory System [Help]

I am trying to save player inventories (a custom system, not Roblox inventory) and it looks like it is saving the data but for some reason I cannot load the data.

I have Studio API Usage on and to add items to the players inventory I add an int value (in server mode) with a value of 1 (which matches the stone’s id).

There are no errors, if you can spot anything that helps please let me know.


Inventory Core:

--: Get Services
local players = game:GetService("Players")

--: Get Modules
local inventoryModule = script.InventoryModule
local invMod = require(inventoryModule)

--: On Player Entry
players.PlayerAdded:Connect(invMod.createPlayerInventory)

--: On Player Exit
players.PlayerRemoving:Connect(invMod.endPlayerInventory)

ItemIDModule:

local itemIdModule = {
	
	["Stone"] = {
		["ID"] = 1 
        -- I'll add more details here later.
	}
	
	
}

return itemIdModule

--: [""] = 

Inventory Module:

local inventoryModule = {}

--: Get Services
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local replicatedStorage = game:GetService("ReplicatedStorage")

--: Get Datastore
local inventoryData = dataStoreService:GetDataStore("InventoryData")

--: Get Item IDs
local itemIdModule = replicatedStorage:WaitForChild("ItemIdModule")
local itemMod = require(itemIdModule)

--: User Inventories
local playerInventories = Instance.new("Folder")
playerInventories.Name = ("PlayerInventories")
playerInventories.Parent = replicatedStorage


--: On Player Join
inventoryModule.createPlayerInventory = function(player)
	local userID = player.UserId -- UserID is used as a key.
	
	--: Set Up Player Inventory
	local playerInventory = Instance.new("Folder")
	playerInventory.Name = userID
	playerInventory.Parent = playerInventories
	
	local inventory = Instance.new("Folder")
	inventory.Name = ("Inventory")
	inventory.Parent = playerInventory
	
	local playerIdentifier = Instance.new("StringValue")
	playerIdentifier.Name = ("Player")
	playerIdentifier.Value = player.Name
	playerIdentifier.Parent = playerInventory
	
	--: Load Data
	local loadTable = inventoryData:GetAsync(userID) or nil --[ROBLOX FORUM: LOAD STARTS HERE]
	
	--: Check For Data
	if loadTable then
		print(loadTable) -- Prints
		
		--: Prepare Item ID
		for slot, id in pairs(loadTable) do
			print("Loading ID") -- Won't print
			local unkItem = Instance.new("IntValue")
			unkItem.Value = id
			
			--: Prepare Item Name 
			for item, info in pairs(itemMod) do
				if unkItem.Value == info.ID then 
					print("Loading Item") -- Won't print
					unkItem.Name = item
					unkItem.Parent = inventory
				end		
			end
		end
	end

	--: Alert
	print("A new player inventory for "..player.Name.."/"..userID..", was set up!")
end

--: On Player Exit
inventoryModule.endPlayerInventory = function(player)
	local userID = player.UserId

	--: Locate Inventory
	local playerFolder = playerInventories:FindFirstChild(userID)
	local playerInventory = playerFolder:FindFirstChild("Inventory")
	
	--: Get Contents
	local contents = playerInventory:GetChildren()
	local saveTable = {}
	
	--: Find & Insert ItemIDs
	for index, item in pairs(contents) do
		if item:IsA("IntValue") then
			print("Saving Item ID: "..item.Value) -- Prints
			local itemID = item.Value
			table.insert(saveTable, itemID)
		end
	end
	
	--: Save Data & Alert
	inventoryData:SetAsync(userID, saveTable)
	print("A player inventory for "..player.Name.."/"..userID..", was saved.")
	
end

return inventoryModule

Hierarchy :
image


Hierarchy In Play Mode:
image

3 Likes

UPDATE:
Looks like this is a problem with saving data, as printing the table returns an empty table.

Resolved by: FloweryMonkeyBoy5 via Discord

Problem:
Added a :BindToClose function (link) to Inventory Core


Iventory Core new:

--: Get Services
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local replicatedStorage = game:GetService("ReplicatedStorage")

--: Get Modules
local inventoryModule = script.InventoryModule
local invMod = require(inventoryModule)

--: On Player Entry
players.PlayerAdded:Connect(invMod.createPlayerInventory)

--: On Player Exit
players.PlayerRemoving:Connect(invMod.endPlayerInventory)

--: Bind To Close
game:BindToClose(function()
  for _,plr in ipairs(players:GetPlayers()) do
    invMod.endPlayerInventory(plr)
  end
end)
1 Like