How to change a saved Table of Values into individual BoolValues under a Player at game start

Hello All,

A Table data conversion question, starting with the setup scenario…

I have a bunch of areas in my game. Players can unlock areas by paying with in-game money. To track which areas have been unlocked, I am storing the information within a Folder under each Player. The Folder contains a BoolValue for each area, with true meaning the area has been unlocked. This information gets saved as a Table within DataStore. When a Player loads into the game I want the table information to converted back into the BoolValues under the aforementioned Folder.

Here’s an example of my DataStore table:

local success, errorMsg = pcall(function()
		areaUnlockedTable = datastore:GetAsync(areaUnlockedKey)
end)


datastore:SetAsync(areaUnlockedKey, {
			area1Save = false,
			area2Save = false,
			area3Save = false,
			area4Save = false,
			area5Save = false,
			area6Save = false,
			area7Save = false,
			area8Save = false,
			area9Save = false}

The Folder that parents the boolValues is created as such:

local unlockedAreas = Instance.new("Folder")
	unlockedAreas.Name = "UnlockedAreas"
	unlockedAreas.Parent = player

To create the BoolValues from the table, I have always typed out a unique set of code for each Table Value, but that takes forever and seems contrary to smart scripting. Here’s an example of how I would create an individual BoolValue for a Table entry:

local area1Unlocked = Instance.new("BoolValue")
	area1Unlocked.Parent = player.UnlockedAreas
	area1Unlocked.Name = "Area1"
	area1Unlocked.Value = areaUnlockedTable.area1Save

My question is, is there a way to save time and typing using some efficient code? I want to turn the Table information into BoolValues placed within the given Folder. I was thinking of using a For Loop but playing with Tables in that way is beyond my current familiarity.

A follow-up question is, is there an efficient way to turn the BoolValues back into a table when saving to the DataStore? Again I’ve always typed it out long-hand.

Thanks in advance!

for key, value in pairs(dataTable) do
    local areaUnlocked = Instance.new("BoolValue")
    areaUnlocked.Name = key
	areaUnlocked.Value = value
	areaUnlocked.Parent = player.UnlockedArea
end

Have you tried this?

Haha…that looks too easy! I’ll give it a shot. Any thoughts about how to reverse it back into a table…even if I had to change the key names in the table?

To change it back into a table:

local dataToSave = {}
for _, value in pairs(unlockedAreas:GetChildren()) do
    dataToSave[value.Name] = value.Value
end

Your suggestions are great! Thanks for the help. You’ve saved me a lot of time and frustration!