0% found this document useful (0 votes)
10 views3 pages

Message

Uploaded by

sleezyezzy09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Message

Uploaded by

sleezyezzy09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

-- 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 = Instance.new("RemoteEvent")
RemoteEvent.Name = "InventoryRemoteEvent"
RemoteEvent.Parent = ReplicatedStorage

-- DataStore
local InventoryDataStore = DataStoreService:GetDataStore("InventoryDataStore")

-- Item Class
local Item = {}
Item.__index = Item

function Item.new(name, description, category)


local self = setmetatable({}, Item)
self.Name = name
self.Description = description
self.Category = category
return self
end

function Item:ToTable()
return {
Name = self.Name,
Description = self.Description,
Category = self.Category
}
end

-- Inventory Class
local Inventory = {}
Inventory.__index = Inventory

function Inventory.new()
local self = setmetatable({}, Inventory)
self.Items = {}
return self
end

function Inventory:AddItem(item)
table.insert(self.Items, item)
end

function Inventory:RemoveItem(name)
for i, item in ipairs(self.Items) do
if item.Name == name then
table.remove(self.Items, i)
break
end
end
end
function Inventory:GetItems()
local itemsTable = {}
for _, item in ipairs(self.Items) do
table.insert(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)
self.Items = {}
for _, itemData in ipairs(itemsTable) do
local item = Item.new(itemData.Name, itemData.Description,
itemData.Category)
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[player.UserId] then
local inventory = Inventory.new()
inventory:LoadFromDataStore(player.UserId)
playerInventories[player.UserId] = inventory
end
return playerInventories[player.UserId]
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 = Item.new(itemData.Name, itemData.Description, itemData.Category)
inventory:AddItem(item)
inventory:SaveToDataStore(player.UserId)
RemoteEvent:FireClient(player, "UpdateInventory", inventory:GetItems())
end

local function OnItemRemoveRequest(player, itemName)


local inventory = GetPlayerInventory(player)
inventory:RemoveItem(itemName)
inventory:SaveToDataStore(player.UserId)
RemoteEvent:FireClient(player, "UpdateInventory", inventory:GetItems())
end

-- Player Added Event


Players.PlayerAdded:Connect(function(player)
-- Listen to RemoteEvents from client
RemoteEvent.OnServerEvent: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


Players.PlayerRemoving:Connect(function(player)
local inventory = playerInventories[player.UserId]
if inventory then
inventory:SaveToDataStore(player.UserId)
playerInventories[player.UserId] = nil
end
end)

You might also like