DataStore2 Inventory Save

Hello I have a Datastore2 Inventory Save that saves but sometimes it loses information, and when I try adding new swords inside the folder for the tools that save, it does not save! Why does this Datastore keep losing information?

game.Players.PlayerAdded:Connect(function(player)
	local data
	local module = require(1936396537)
	local tools = module("tools", player)
	data = tools:Get({})

	if typeof(data) == "table" then
		for _, toolName in next, data do
			local tool = game.ServerStorage.Tools:FindFirstChild(toolName)

			if player.Backpack:FindFirstChild(tool.Name) then
				return
			end

			if tool then
				local newTool = tool:clone()
				newTool.Parent = player.Backpack

				local newTool = tool:clone()
				newTool.Parent = player.StarterGear
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local toolsTable = {}

	local module = require(1936396537)
	local tools = module("tools", player)

	for _, tool in next, player.Backpack:children() do
		if game.ServerStorage.Tools:FindFirstChild(tool.Name) then
			local newTable = tools:Get()
			table.insert(newTable, tool.Name)
			tools:Set(newTable)
		end
	end
end)
1 Like

According to the DataStore2 documentation:

DataStore2 is built to be used whenever your data actually changes. You shouldn’t invoke DataStore2 in PlayerRemoving at all.

So how would I prevent this and be able to save the system better? The other problem is there are no examples of Inventory saving systems, and I want to know how to improve mine.

You can save the players inventory after you add an item to their inventory.

Okay so I would only delete the second part where the game.Players.PlayerRemoving area and add that instead?

What you would do is remove the PlayerAdded event and then invoke DataStore2’s Save method after you distribute the new item to the players inventory.

Why can’t I just remove the game.Player.PlayerRemoving and just add something like if player adds a tool then I can just use what I had underneath?

If you’re referring to this and you know for a fact it works, then yes you could also do that.

Okay I will try that right now. Thanks!

Would there be a better way of making sure all the items saved? Like what if I just replaced the playerremoved with playeradded would this work then?

That would be kind of redundant because you don’t really need to save a player’s data when it loads.

You could make a utility module for saving data. A cool usage would be something like you can pass the type of data you want to save and let the module save that specific data type.

-- module.lua
local DataStore2 = require(path.to.datastore2)

local Module = {}

function Module:Save(Player, DataType) -- invoke this method after you update the players inventory
  if DataType == "Inventory" then
    -- iterate through the players inventory and save it using datastore2
    
  end
end

return Module

Wait so that would actually know what Inventory already is? Then I could just do this and then be able to save because it was datatype inventory:

-- module.lua
local DataStore2 = require(path.to.datastore2)

local Module = {}

function Module:Save(Player, DataType) -- invoke this method after you update the players inventory
  if DataType == "Inventory" then
    -- iterate through the players inventory and save it using datastore2
   local toolsTable = {}

	local module = require(1936396537)
	local tools = module("tools", player)

	for _, tool in next, player.Backpack:children() do
		if game.ServerStorage.Tools:FindFirstChild(tool.Name) then
			local newTable = tools:Get()
			table.insert(newTable, tool.Name)
			tools:Set(newTable)
		end
	end
end)
    
  end
end

return Module

You can reference the inventory using the Player object passed as a parameter.

This is prototyped code and you will have to modify it to fit your use case, this is just a general idea:

-- module.lua
local DataStore2 = require(path.to.datastore2)

local Module = {}

function Module:Save(Player, DataType) -- invoke this method after you update the players inventory
local ToolsTable = {}

  if DataType == "Inventory" then
    	for _, Tool in next, Player.Backpack:GetChildren() do
		  if not Tool:IsA("Tool") then return end
            table.insert(ToolsTable, Tool.Name)
         end
	 end
        -- invoke datastore2's save method on the table
end

return Module

Okay I will try that. So the script you just made just puts the tools in the table but did not save it right? So I would then use the saving way? Also the saving way is pretty confusing. I am not sure whcih is the actual one.

But yes, the script just puts the tool in the table and does not save it.

Okay Thanks! I just do not understand what the saving method is. I could not find it in the article excatly. There are mutliple ways.

game.Players.PlayerAdded:Connect(function(player)
	local data
	local module = require(1936396537)
	local tools = module("tools", player)
	data = tools:Get({})

	if typeof(data) == "table" then
		for _, toolName in next, data do
			local tool = game.ServerStorage.Tools:FindFirstChild(toolName)

			if player.Backpack:FindFirstChild(tool.Name) then
				return
			end

			if tool then
				local newTool = tool:clone()
				newTool.Parent = player.Backpack

				local newTool = tool:clone()
				newTool.Parent = player.StarterGear
			end
		end
	end
end)


local plyaer = game.Players.LocalPlayer
local Inventory = plyaer:WaitForChild("Backpack")
if Inventory:FindFirstChild(tool.Name) then
	local toolsTable = {}

	local module = require(1936396537)
	local tools = module("tools", player)

	for _, tool in next, player.Backpack:children() do
		if game.ServerStorage.Tools:FindFirstChild(tool.Name) then
			local newTable = tools:Get()
			table.insert(newTable, tool.Name)
			tools:Set(newTable)
		end
	end
end)

Would this work?

I have no way if knowing if that will work or not. You can test it yourself and post any errors here.


Does not work. I lose all my shop tools because maybe an error with the end.