DataStore isn't saving a table

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:
image

Well, it seems like the issue might be with the way you’re handling the inventory table. In your PlayerAdded function, you’re not actually assigning the result of inventoryStore:GetAsync(Player.UserId) to localInventory. You should update that part of the code to:

local success, result = pcall(function()
    localInventory = inventoryStore:GetAsync(Player.UserId)
end)
1 Like

UPD: I just moved module script functions into Server Script and it started working

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.