I am building an Inventory system for my game. Currently, I have a ModuleScript in ReplicatedStorage which contains the player’s inventory data in a table.
When the player exits the game, the data from the player’s inventory table in the ModuleScript is saved to the datastore and cleared from the ModuleScript.
When the player rejoins the game, the inventory data is retrieved from the datastore and saved to the ModuleScript.
The only time data is read/saved to the Datastore is upon players joining/exiting the game. The inventory is managed in the game only by using the ModuleScript’s functions. I want to keep it this way to minimize API calls and simplify my code. Are there any issues that I could run into with this strategy?
-- Inventory Cache
local InventoryCache = require(ReplicatedStorage:WaitForChild("Cache"):WaitForChild("InventoryCache"))
-- When the player joins, get their inventory data from the datastore and add it to the PlayerInventoryData table.
-- Also get their inventory size from the datastore and add it to the PlayerInventoryMaxSize table.
Players.PlayerAdded:Connect(function(player)
local success, inventory_data = pcall(function()
return InventoryStore:GetAsync(("Inventory_"..(tostring(player.UserId))))
end)
if success then
if inventory_data == nil then
InventoryCache.SetPlayerInventory(player.Name, {})
else
InventoryCache.SetPlayerInventory(player.Name, inventory_data)
end
end
local success, inventory_max_size = pcall(function()
return InventorySizeStore:GetAsync(player.UserId)
end)
if success then
if inventory_max_size == nil then
InventoryCache.SetInventoryMaxSize(player.Name, 20)
else
InventoryCache.SetInventoryMaxSize(player.Name, inventory_max_size)
end
end
end)
-- When the player leaves, save their inventory and inventory size to the datastore.
Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
InventoryStore:SetAsync(("Inventory_"..(tostring(player.UserId))), InventoryCache.GetPlayerInventory(player.Name))
end)
if not success then
print(errorMessage)
end
local success, errorMessage = pcall(function()
InventoryStore:SetAsync(player.UserId, InventoryCache.GetInventoryMaxSize(player.Name))
end)
if not success then
print(errorMessage)
end
-- Remove the player's cached data.
InventoryCache.RemovePlayerDataFromCache(player.Name)
end)
Any feedback would be appreciated. I want to eventually make a guide that includes each part needed to build an inventory system.