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)
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.
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.
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.
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)