-- Advanced Inventory System with Categories and Saving
-- Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
-- Remote Event
local RemoteEvent = [Link]("RemoteEvent")
[Link] = "InventoryRemoteEvent"
[Link] = ReplicatedStorage
-- DataStore
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")
-- Item Class
local Item = {}
Item.__index = Item
function [Link](name, description, category)
local self = setmetatable({}, Item)
[Link] = name
[Link] = description
[Link] = category
return self
end
function Item:ToTable()
return {
Name = [Link],
Description = [Link],
Category = [Link]
}
end
-- Inventory Class
local Inventory = {}
Inventory.__index = Inventory
function [Link]()
local self = setmetatable({}, Inventory)
[Link] = {}
return self
end
function Inventory:AddItem(item)
[Link]([Link], item)
end
function Inventory:RemoveItem(name)
for i, item in ipairs([Link]) do
if [Link] == name then
[Link]([Link], i)
break
end
end
end
function Inventory:GetItems()
local itemsTable = {}
for _, item in ipairs([Link]) do
[Link](itemsTable, item:ToTable())
end
return itemsTable
end
function Inventory:SaveToDataStore(playerId)
local success, err = pcall(function()
local itemsTable = self:GetItems()
local jsonData = HttpService:JSONEncode(itemsTable)
InventoryDataStore:SetAsync(tostring(playerId), jsonData)
end)
if not success then
warn("Failed to save inventory for player " .. tostring(playerId) .. ":
" .. err)
end
end
function Inventory:LoadFromDataStore(playerId)
local success, jsonData = pcall(function()
return InventoryDataStore:GetAsync(tostring(playerId))
end)
if success and jsonData then
local itemsTable = HttpService:JSONDecode(jsonData)
[Link] = {}
for _, itemData in ipairs(itemsTable) do
local item = [Link]([Link], [Link],
[Link])
self:AddItem(item)
end
else
warn("Failed to load inventory for player " .. tostring(playerId) .. ":
" .. (jsonData or "No data"))
end
end
-- Player Inventory Management
local playerInventories = {}
local function GetPlayerInventory(player)
if not playerInventories[[Link]] then
local inventory = [Link]()
inventory:LoadFromDataStore([Link])
playerInventories[[Link]] = inventory
end
return playerInventories[[Link]]
end
local function OnInventoryRequest(player)
local inventory = GetPlayerInventory(player)
RemoteEvent:FireClient(player, "UpdateInventory", inventory:GetItems())
end
local function OnItemAddRequest(player, itemData)
local inventory = GetPlayerInventory(player)
local item = [Link]([Link], [Link], [Link])
inventory:AddItem(item)
inventory:SaveToDataStore([Link])
RemoteEvent:FireClient(player, "UpdateInventory", inventory:GetItems())
end
local function OnItemRemoveRequest(player, itemName)
local inventory = GetPlayerInventory(player)
inventory:RemoveItem(itemName)
inventory:SaveToDataStore([Link])
RemoteEvent:FireClient(player, "UpdateInventory", inventory:GetItems())
end
-- Player Added Event
[Link]:Connect(function(player)
-- Listen to RemoteEvents from client
[Link]:Connect(function(player, action, data)
if action == "RequestInventory" then
OnInventoryRequest(player)
elseif action == "AddItem" then
OnItemAddRequest(player, data)
elseif action == "RemoveItem" then
OnItemRemoveRequest(player, data)
end
end)
end)
-- Player Removing Event
[Link]:Connect(function(player)
local inventory = playerInventories[[Link]]
if inventory then
inventory:SaveToDataStore([Link])
playerInventories[[Link]] = nil
end
end)