How to make a Strong DataStore

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.

1 Like

By strong I mean a DataStore that does not lose information easily.

You should not be using DataStore:UpdateAsync() for getting data. Instead, you should be using DataStore:GetAsync().

Also, why are you using pcalls if you are not going to utilize the error message if there is one?

It was originally GetAsync but I changed it because UpdateAsync in what the articles said.

How is UpdateAsync better? GetAsync and UpdateAsync do different things.

Also, can you provide the article?

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

It was the forever article.

That post is talking about SetAsync, not GetAsync.

Oh yeah, thanks. How can I make my datastore stronger like lose not as much information?

How would I do use the error functions?

You should check if the function is successful, and if not, retry until it succeeds.

Here is a post about pcalls:

1 Like

Well first of all i recommend using profileservice module it handles everything i described and its free. For your question, check this article Pcalls - When and how to use them - Resources / Community Tutorials - Roblox Developer Forum it also has a example for datastores

What is the profile service module?

Okay Thanks! I am only changing my datastore because I keep losing information.

ProfileService is a module that loads and saves data. Here is a post about ProfileService:

Here you go



Not sure if it does not work anymore, but may be because I leave too quickly.

I dont understand what do you mean you just gave me images

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)

image