DataStore { Multiple Values }

Hi, I’m new to DataStores and working with them - I’ve successfully created a DSS that saves the values shown below; however - as the player progresses through the game I aim for values ( that didn’t exist prior ) to be added to the ‘Unlocked’ folder.

Example; Player unlocks a ship - The ships name is added to the ‘unlocked’ folder for future preferences.

How could I go about doing this?

image

Could you show us the code you already have?

What if you used a boolvalue? If it is set to false, it is unowned, if it is set to true, it is owned.

1 Like
function System.DataSave(Player)
		PlayerData = Player:WaitForChild("PlayerData")
		
		local Saves = {
			PlayerData["isPlaying"].Value,
			PlayerData["CharacterData"]["Level"].Value,
			PlayerData["CharacterData"]["Level"]["Exp"].Value,
			PlayerData["CharacterData"]["Level"]["MaxExp"].Value,
			PlayerData["StarShipData"]["Exterior"].Value,
			PlayerData["StarShipData"]["Interior"].Value}
		
		DS1:SetAsync(Player.UserId, Saves)
		
	end

You can go through all of the values by saying :GetDescendants, then check if its a userdata value, and if it is, add it to the dictionary.

This is a way of doing it - However, I’m looking to spare the time by having the new names inserted themselves as opposed to naming, organising and preparing several ships throughout time.

I would totally rework the heirachy of how you’re saving that data, so it is easier to save the unlocked stuff:

function System.DataSave(player)
  local PlayerData = player:WaitForChild('PlayerData')
  local Saves = {}

  Saves.isPlaying = PlayerData.isPlaying.Value

  local configs = {'CharacterData', 'StarShipData', 'Unlocked'}
  for _, config_name in pairs(configs) do
    local config = PlayerData[config_name]
    Saves[config_name] = {}

    for _, ValueObject in pairs(config:GetDescendants()) do
      Saves[config_name][ValueObject.Name] = ValueObject.Value
    end
  end

  DS1:SetAsync(Player.UserId, Saves)

end

This would result in a Saves table like this:

Saves = {
  isPlaying = true;
  CharacterData = { Level = 1, Exp = 1, MaxExp = 1 };
  StarShipData = { Exterior = ?, Interior = ?};
  Unlocked = { ship_name = ship_value }:
}

I know this is a lot different to your code - ask me any questions!

2 Likes

Adore this method!

If I were to now re-write the loading format, how would I adjust this to compliment the changes?

function System.DataLoad(Player)
		PlayerData = Player:WaitForChild("PlayerData")
		
		local isData = DS1:GetAsync(Player.UserId)
		
		if isData then
			PlayerData["isPlaying"].Value = isData[1]
			
			PlayerData["CharacterData"]["Level"].Value = isData[2]
			PlayerData["CharacterData"]["Level"]["Exp"].Value = isData[3]
			PlayerData["CharacterData"]["Level"]["MaxExp"].Value = isData[4]
			
			PlayerData["StarShipData"]["Exterior"].Value = isData[5]
			PlayerData["StarShipData"]["Interior"].Value = isData[6]
		else
			---------
		end
		
	end
1 Like

I’ll let you try figure it out, but I’ll start by doing the first few…

function System.DataLoad(Player)
		PlayerData = Player:WaitForChild("PlayerData")
		
		local isData = DS1:GetAsync(Player.UserId)
		
		if isData then
			PlayerData["isPlaying"].Value = isData['isPlaying']
			
			PlayerData["CharacterData"]["Level"].Value = isData['CharacterData']['Level']
			PlayerData["CharacterData"]["Level"]["Exp"].Value = isData[3]
			PlayerData["CharacterData"]["Level"]["MaxExp"].Value = isData[4]
			
			PlayerData["StarShipData"]["Exterior"].Value = isData[5]
			PlayerData["StarShipData"]["Interior"].Value = isData[6]
		else
			---------
		end
		
	end

From the saving code, I’m sure you can find a more compact way of loading it rather than typing each one individually :wink:

3 Likes