Hello !
This is simple inventory system that i made in an hour, the thing is everything is working fine except data saving, DataStore just doesnt wanna save provided table. (Every other DataStore ingame is working)
Script:
local dataStoreService = game:GetService("DataStoreService")
local inventoryStore = dataStoreService:GetDataStore("InventoryStore0")
local InventoryModule = require(game.ServerStorage.Inventory)
game.Players.PlayerAdded:Connect(function(Player)
local localInventory = InventoryModule.New()
local success, result = pcall(function()
inventoryStore:GetAsync(Player.UserId)
end)
if result ~= nil then
localInventory=result
print("Loaded inventory")
else
print("Inventory was not loaded")
InventoryModule:AddItem(localInventory,'Apple',1)
end
local function Save()
print(localInventory)
local success, result = pcall(function()
inventoryStore:SetAsync(Player.UserId,localInventory)
end)
print(success)
print(result)
end
game:BindToClose(function()
Save()
end)
end)
Module:
local Inventory = {}
function Inventory.New()
return {}
end
function Inventory:AddItem(GivenInventory,Item,Amount)
if GivenInventory[Item] ~= nil then
GivenInventory[Item]+=1
else
GivenInventory[Item]=Amount
end
end
function Inventory:RemoveItem(GivenInventory,Item,Amount)
if GivenInventory[Item] ~= nil and GivenInventory[Item] >= 1 then
if GivenInventory[Item]-1 <= 0 then
GivenInventory[Item]=nil
else
GivenInventory[Item]-=1
end
end
end
return Inventory
Output: