Saving folder of instances to data store

I want to create a script to save and load data for my tycoon game, but I don’t know where to start, I have a folder where buttons a player purchased will go to.

image

I want to save the names of all the purchased buttons into an array and save it into the datastore so it looks something like this when loading from datastore:

{'BunkerBlueprint', 'BunkerExteriorWalls', 'BunkerFloor', 'BunkerInteriorWalls'}

How would I got about this? Any help is appreciated :slight_smile:

1 Like

I assume you already know how to save tables in DataStore, if not I would suggest reading up on it or use DataStore2. When that is said.

The idea for saving is:
When a player leaves, you loop through all the important folders, and insert them into a table like below, after that, save that table.

local PurchasedButtons = ..--Define yourself
local DataTable = .. -- Define yourself
local AllPurchases = {}
for _, Purchase in pairs(PurchasedButtons:GetChildren()) do
	table.insert(AllPurchases,Purchase.Name)
end
print(AllPurchases) -- This is the table you save.

The idea for loading is:
When a player joins, you loop through the dataTable you’ve saved (if there is any), and check what they’ve bought so far.

local TemplateButtons = ..-- Define yourself
local PurchasedButtons = ..--Define yourself
local DataTable = .. -- Define yourself
for _, Purchase in pairs(DataTable) do
	local FindPurchase = TemplateButtons:FindFirstChild(Purchase)
	if FindPurchase then
		local New = FindPurchase:Clone()
		New.Parent = PurchasedButtons
	end
end
2 Likes

I was actually trying to do this in the first place but instead of doing table.insert I was doing the name of the table then .insert. Thanks for this though, it is working perfectly!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.