How to make a Strong DataStore

@takticiadam You said it was correct here:


@InquistorWasBanned It is not working because the data is not loading.

Oh okay, so how do I load the data?

I think you didnt understand what I meant there, first of all why would you except me to say its incorrect then say its correct out of nowhere, what I meant there is OP is using pcall’s without handling the outcome.

Well change it to :GetAsync, thats it.

Okay let me try it right now. Do you have discord?

no, I do not have discord thing

Yes it works. But I am afraid the saving might still lose a lot of information. How can I minimize that?

If you still lose information, its probably roblox’s fault as i said. You can do nothing about that just blame them if you want to. If you really want to minimize it just use profileservice.

Okay Thanks I will try using ProfileService.

You could minimize data loss by having a retry function if the pcall failed.

How would I do that and where?

There is an example in the post I linked earlier under the DataStore errors - retrying section.

So the more pcalls I add the more strong it will be?

No, that is not what I meant. The example keeps calling the call until it returns success.

local success, responce 
local count = 0
game.Players.PlayerAdded:Connect(function(plr)




repeat
		
		if count >=1 then
			warn("Retrying")
			wait(7)
		end
			success, responce = pcall(DataStore.GetAsync, DataStore, "MyDataStore")
			count = count + 1
		until success
	
		for _, toolName in pairs() 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)

You can cache a DataStore for each user while they’re in game and save it when they leave. That way you can make as many transactions to the cached data as you like without actually interacting with the DataStoreService itself.

Just store a table where the key is the Player and the value is their data to save when they join. Once they leave the game, then save to their DataStore key and remove their data from your cache.

Don’t forget to save everyone’s data (who is still left in the cache) when the game shuts down. This is basically how these DataStore modules work.

DataStore:UpdateAsync() can also be used to retrieve data, doesn’t matter.

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

Do you not know how a pcall works? GetAsync or UpdateAsync are asynchronous functions, hence the Async at the end of their name. They send a GET HTTP request to retrieve the data from an API which will yield the current thread until there is a response from the API, there is none, it returns nil.

Asynchronous functions should always be wrapped in a pcall since they are prone to failing. The main purpose of a pcall is to prevent the script from breaking if there is an error on the function you pcall, not for checking any errors but should.

DataStore:UpdateAsync() can be used to retrieve data, but it is meant to update the old data with new data.

I know how a call works; he was not handling the errors if there were any.

I know how a call works; he was not handling the errors if there were any.

He is wrapping the data call into a pcall which is my point here and yes, he should handle the errors if there were any returned by the pcalled call. However that doesn’t prove your point that he was using pcalls unnecessarily as by your post:

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

So was I not doing it correctly?