Convert this data system from SetAsync to UpdateAsync

I wanna convert my data store system to use UpdateAsync, however, I’ve never used it before, as SaveAsync has never caused me problems in the past, but everyone says I should change it. I’ve tried to use DataStore2, but that was way too complex for what I needed (all the tutorials only taught how to save individual values)

Problem with my datastore is I have multiple mixed values, inluding tables within tables, etc.

Here’s how my data is structured

return {
	-- Main Data
	Coins = 100000,
	
	Level = 1,
	Exp = 0,
	
	FavouriteColor = {107, 184, 217},
	
	Codes = {},
	
	StarterPackUsed = false,
	
	Quests = {},
	
	-- House Data
	House = 'Default',	
		
	Houses = {
		['Default'] = {
			Exterior = {},
			House = {},
			Purchases = {}
		},
	},
	
	Furniture = {},
	
	-- Character Data
	Character = {
		Name = '',
		Age = 'Adult',
		HatAccessories = {},
		HairAccessories = {},
		FaceAccesssories = {},
		NeckAccessories = {},
		ShoulderAccessories = {},
		BackAccesssories = {},
		WaistAccesssories = {},
		Face = '',
		Shirt = '',
		Pants = '',
		Color = 'Light orange',
	},
}

And when I wanna update say the players cash I’d just go into their data and do like

local User = PlayerData[Player.UserId]
if not User then return end

User.Cash = User.Cash + 10

And it’d basically be the same for all data updates.

local User = PlayerData[Player.UserId]
if not User then return end

User.House = 'Other House'
-- etc...

Problem lies with the ‘Houses’ table. To get that I got all the items inside the players house, stored it all in tables, named, etc. and then placed those respective tables under each section (Exterior, House, Purchases) and it worked great. And to save all I do is just use to SetAsync when the player leaves

So I fear using UpdateAsync would just cause more nightmares than solutions.

Please for the love of logic, DO NOT just link me to UpdateAsync pages/forum posts. I’ve read them all, and then re-read them. They haven’t helped thus far.

For those who are curious how I save the Houses data
local Items = {Exterior = {}, House = {}, Purchases = {}}
	
	for _, v in pairs(PlayersPlot.Plot:GetDescendants()) do
		if v:IsA('BrickColorValue') then
			Items.Exterior[v.Parent.Name] = (v.Value).Name
		end
	end

	for _, item in pairs(PlayersHouse.House.Purchases:GetChildren()) do
		local PurchaseData = {}
		local PrimaryPart = item.PrimaryPart
		local Position = PlayersHouse.PrimaryPart.CFrame:inverse() * PrimaryPart.Position
		local Direction = PrimaryPart.CFrame.lookVector
		PurchaseData.ItemName = item.Name
		PurchaseData.Position = {
			Position.X, 
			Position.Y, 
			Position.Z, 
			Direction.X, 
			Direction.Y, 
			Direction.Z
		}
		
		local Primary = item:FindFirstChild('Primary')
		PurchaseData.Primary = Primary.BrickColor.Name
		
		local Secondary = item:FindFirstChild('Secondary')
		if Secondary then
			PurchaseData.Secondary = Secondary.BrickColor.Name
			
			local Tertiary = item:FindFirstChild('Tertiary')
			if Tertiary then
				PurchaseData.Tertiary = Tertiary.BrickColor.Name
			end
		end
		
		Items.Purchases[#Items.Purchases + 1] = PurchaseData
	end
	
	for _, item in pairs(PlayersHouse.House.House:GetDescendants()) do
		if item:IsA('BasePart') or item:IsA('UnionOperation') then
			local HouseData = {}
			HouseData.ItemName = item.Name
				
			HouseData.Texture = item.Texture.Texture
	
			Items.House[#Items.House + 1] = HouseData
		end
	end

	local User = PlayerData[player.UserId]
	if not User then return end

	User.Houses[User.House] = Items
3 Likes

Hi there!

First of all, I recommend trying again with DataStore2. It makes it much easier to work with tables. If you have questions about it, feel free to shoot me a message! I’ll be happy to help you out. Here’s some code examples to get you started:

Saving Data

For saving data, I use a module to convert folders and tables seamlessly, which can be found at:
https://www.roblox.com/library/590549414/OAuth2-Table-Module
DS2 can be found at https://www.roblox.com/library/1936396537/DataStore2-Data-Loss-Prevention-and-Caching
Using these modules, we can easily handle data with the following code examples:

First, in order to save data, we need to reference the folder to save.

local playerData = DataConverter.convert(folder) -- This will make playerData into a table that we can then easily pass to the DataStore2 module to be saved.
local playerDataStore = DataStore2("Data", player)

playerDataStore:Set(playerData)
Loading Data

Loading data is just as simple as saving it. My implementation of saving data uses both the modules I mentioned earlier.

local playerDataStore = DataStore2("Data", player)
local playerData = PlayerDataStore:GetTable(DefaultData) -- GetTable is a function of DS2 that accepts a table value to assess as the default table. If the player's data doesn't contain values listed in the default table, DS2 will add them in!

local playerDataFolder = Converter.convertr(playerData)
playerDataFolder.Name = "PlayerData"
playerDataFolder.Parent = player -- Can save wherever, but this will put a folder under the player object that can then be modified with other scripts.
Modifying Data

At this point, modifying data is as simple as changing userdata values.

game:GetService("Players")[player.Name].PlayerData.Cash.Value = 100 -- Sets cash to 100

I hope this shed some light on saving data for you, and feel free to approach me with any questions. Apologies if there is any mangled broken code, this is my first in depth support post!

2 Likes