Best way to store this?

Hello,

I’ve been working on my data saving systems for a game I’m working on, this specific code is from the script that handles the player’s Job Data(ie: their salary per interaction, exp, level and all time earnings). Since theres multiple categories of jobs and I need data for each of them(currently Bartender and Cashier), I decided to store the data in a matrix(arrays inside of an array) and then save it to my data store 2. I wanted to know if this is the most efficient way of going about this or if theres other solutions to it.

Heres the code itself:

local DataStore2 = require(script:WaitForChild("DataStore2"))

game.Players.PlayerAdded:Connect(function(plr)
		
	local DS = DataStore2("OccupationData",plr)
	
	local mainFold = Instance.new("Folder",plr)
	mainFold.Name = "OccupationData"

	local cashierFold = Instance.new("Folder",mainFold)
	cashierFold.Name = "CashierData"

	local cashierLevel = Instance.new("NumberValue",cashierFold)
	cashierLevel.Name = "CashierLevel"

	local cashierEXP = Instance.new("NumberValue",cashierFold)
	cashierEXP.Name = "CashierExperience"
	
	local cashierSalary = Instance.new("NumberValue",cashierFold)
	cashierSalary.Name = "CashierSalary"

	local cashierATE = Instance.new("NumberValue",cashierFold)
	cashierATE.Name = "CashierAllTimeEarnings"

	local bartenderFold = Instance.new("Folder",mainFold)
	bartenderFold.Name = "BartenderData"

	local bartenderLevel = Instance.new("NumberValue",bartenderFold)
	bartenderLevel.Name = "BartenderLevel"

	local bartenderEXP = Instance.new("NumberValue",bartenderFold)
	bartenderEXP.Name = "BartenderExperience"

	local bartenderSalary = Instance.new("NumberValue",bartenderFold)
	bartenderSalary.Name = "BartenderSalary"

	local bartenderATE = Instance.new("NumberValue",bartenderFold)
	bartenderATE.Name = "BartenderAllTimeEarnings"
	
	if DS:Get() then
		
		local data = DS:Get()
				
		cashierLevel.Value = data[1][1]
		cashierEXP.Value = data[1][2]
		cashierSalary.Value = data[1][3]
		cashierATE.Value = data[1][4]
		
		bartenderLevel.Value = data[2][1]
		bartenderEXP.Value = data[2][2]
		bartenderSalary.Value = data[2][3]
		bartenderATE.Value = data[2][4]
		
	else

		cashierLevel.Value = 1
		cashierEXP.Value = 0
		cashierSalary = .5
		cashierATE.Value = 0	
		bartenderLevel.Value = 1
		bartenderEXP.Value = 0
		bartenderSalary.Value = 1
		bartenderATE.Value = 0
		
		DS:Set({
			{1,0,0.5,0};
			{1,0,1,0}
		})
				
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local DS = DataStore2("OccupationData",plr)
	
	local data = {
		{
			plr.OccupationData.CashierData.CashierLevel.Value,
			plr.OccupationData.CashierData.CashierExperience.Value,
			plr.OccupationData.CashierData.CashierSalary.Value,
			plr.OccupationData.CashierData.CashierAllTimeEarnings.Value,
		};
		{
			plr.OccupationData.BartenderData.BartenderLevel.Value,
			plr.OccupationData.BartenderData.BartenderExperience.Value,
			plr.OccupationData.BartenderData.BartenderSalary.Value,
			plr.OccupationData.BartenderData.BartenderAllTimeEarnings.Value,
		};
	}
	
	DS:Set(data)	
end)
1 Like

Whenever I have loads of data that I wish to always replicate, I use a single string value to hold all the data. Why? It prevents from having this much extra code. The creation of all those values, defining them, and not to forget identifying them on the client.

Rather than having all those various variables and values within a folder, I keep it nice and clean with a global data table, typically denoted by _G.PlayerData, along with a single StringValue either parented under the Player itself or a reserved Folder in Workspace/ReplicatedStorage. Now, that’s just personal preference, however I find it very useful for organization.

How can this be accomplished? HttpService:JSONEncode(Data) can transform a set of data into a single string value. The read-from value can then be decoded on the client by HttpService:JSONDecode(String). Simply alter the table data and then replicate that with the encode method to the string value. Not very tricky, and very helpful.

Hope this helps.

1 Like

This seems like a WAY easier method and I’ll absolutely give it a go. I knew a lot of games used strings to store data but never truly understood it. Cheers for this response.

This is far too compressed for the scope of the data you are saving. You would be better off saving yourself from coding pain by saving as such:
Data[“Coins”] = Coins.Value

You don’t need to compress this data. The only time that data compression matters is when you are saving thousands of table indexes with multi-layered tables. Even then, all of this data is stored as strings and numbers.

As far as your folders go, if your creating a PlayerFolder like this, create the Folder yourself and put it under your Server Script, and clone it for every new player. Your data management needs to be this simplified and self-explanatory.