Hello everyone. I am trying to make an Inventory DataStore save that is stronger. I tried using replacing my SetAsync to UpdateAsync but I am not sure if it was used correctly. Finally is there any way I can make it even better?
local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local data
local success, errorMessage = pcall(function()
data = DataStore:UpdateAsync(plr.UserId)
end)
if data ~= nil then
for _, toolName in pairs(data) do
local tool = game.ReplicatedStorage.ToolsSaved:FindFirstChild(toolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = plr.Backpack
local newTool = tool:Clone()
newTool.Parent = plr.StarterGear
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.ToolsSaved:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId,toolsTable)
end)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.ToolsSaved:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId,toolsTable)
end)
end
end)
What do you mean by strong? I recommend using ProfileService because it has session-locking feature that prevents players from duping items in your game.
No matter what you do datastore data is stored on roblox servers, so if you lose information its probably not your but roblox’s problem theres not a lot of much things you can do to making your datastore strong, you can wrap things in pcall, make retry system etc. etc. and that will make your datastore stronger than what was enough
I bought some tools but they did not even save. I have a folder named ToolSaved in Replicated Storage. This may be because it is too quick to save but I am not sure.
local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local data
local success, errorMessage = pcall(function()
data = DataStore:GetAsync(plr.UserId)
end)
if data ~= nil then
for _, toolName in pairs(data) do
local tool = game.ReplicatedStorage.ToolsSaved:FindFirstChild(toolName)
if tool then
local newTool = tool:Clone()
newTool.Parent = plr.Backpack
local newTool = tool:Clone()
newTool.Parent = plr.StarterGear
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.ToolsSaved:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId,toolsTable)
end)
end)
game:BindToClose(function()
for _, plr in pairs(game.Players:GetPlayers()) do
local toolsTable = {}
for _, tool in pairs(plr.Backpack:GetChildren()) do
if game.ReplicatedStorage.ToolsSaved:FindFirstChild(tool.Name) then
table.insert(toolsTable,tool.Name)
end
end
local success, errorMessage = pcall(function()
DataStore:SetAsync(plr.UserId,toolsTable)
end)
end
end)