Need help with datastore

So i want to save all items inside a folder, I already have a datastore for some other values, but I do not know how to add the items inside the folder to a datastore

local players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
DataStore = DataStoreService:GetDataStore('Statstore')
local RunService = game:GetService("RunService")

-- Serialization function
local function serializeObject(object)
	local serializedObject = {
		Name = object.Name,
		ClassName = object.ClassName,
		Properties = {},
		Children = {},
	}

	-- Serialize properties
	for _, property in pairs(object:GetProperties()) do
		local value = object[property.Name]
		serializedObject.Properties[property.Name] = value
	end

	-- Recursively serialize children
	for _, child in pairs(object:GetChildren()) do
		local serializedChild = serializeObject(child)
		table.insert(serializedObject.Children, serializedChild)
	end

	return serializedObject
end

-- Deserialization function
local function deserializeObject(serializedObject)
	local object = Instance.new(serializedObject.ClassName)
	object.Name = serializedObject.Name

	-- Set properties
	for propertyName, value in pairs(serializedObject.Properties) do
		object[propertyName] = value
	end

	-- Recursively deserialize children
	for _, serializedChild in pairs(serializedObject.Children) do
		local child = deserializeObject(serializedChild)
		child.Parent = object
	end

	return object
end

-- load leaderstats and datastore
game.Players.PlayerAdded:Connect(function(player)
	local teams = game:GetService("Teams"):GetChildren()
	for i, team in pairs(teams) do
		print(team.Name)
		local players = team:GetChildren()
		if #players == 0 then
			player.Team = team
		end
	end

	wait()
	local tycoons = game.Workspace.Tycoons:GetChildren()
	for i, tycoon in pairs(tycoons) do
		if tycoon.Name == player.Team.Name then
			tycoon.Owner.Value = player.UserId
		end
	end	
	
	local getSaved = DataStore:GetAsync(player.UserId)

	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local priceSave = Instance.new("Folder",player)
	priceSave.Name = "pricesave"
	
	local Soda = Instance.new("IntValue", leaderstats)
	Soda.Name = "Soda"
	
	local Price = Instance.new("IntValue", priceSave)
	Price.Name = "Price"
	
	local Tycoons = game.Workspace.Tycoons:GetChildren()
	local team = player.Team

	local playerId = "plr_"..player.UserId

	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(playerId)
	end)
	local function tycoonsave(folder)
		local savedtycoon = deserializeObject(folder)
		local tycoons = game.Workspace.Tycoons:GetChildren()
		for i, tycoon in pairs(tycoons) do
			if tycoon.Name == player.Team.Name then
				local savestuff = tycoon.SaveStuff:GetChildren()
				for i, part in ipairs(savestuff) do
					part:Destroy()
				end
				savedtycoon.Parent = tycoon					
			end
		end	
	end

	if success then
		if data then
			Soda.Value = data.soda
			Price.Value = data.price
		end
	else
		Soda.Value = 0
		Price.Value = 0
	end
end)

-- save leaderstats in datastore
game.Players.PlayerRemoving:Connect(function(player)
	local Soda = player.leaderstats.Soda
	local Price = player.pricesave.Price
	local PlayerId = "plr_"..player.UserId
	local Tycoons = game.Workspace.Tycoons:GetChildren()
	local team = player.Team
	local function FindTycoon()
		for i, tycoon in ipairs(Tycoons) do
			if tycoon.Name == team.Name then
				print("tycoon found")
				return tycoon.SaveStuff
			end
		end
	end
	local savestuff = FindTycoon()

	local data = {
		soda = Soda.Value;
		price = Price.Value;
	}

	local success, errormessage = pcall(function()
		DataStore:SetAsync(PlayerId, data)
	end)
end)

--[[ This Avoids Data Loss ]]--
game:BindToClose(function()
	if RunService:IsStudio() then
		wait(7)
	end
end)

Are you trying to save multiple values or multiple values in a table or array?
If trying to save multiple values when you bind to close or setAysnc()

I am trying to save everything in the folder
Screenshot 2022-12-30 022636

So datastores can’t store instances, you’ll just get nil, you can store strings, and values, not instances or objects, you’d have to identify each item with a string and save that, and when you get the data, you can use the string to identify the instance.

How would i go on about doing something like identifying them all with a string?

You’d basically before setting aysnc() you should put the data in a table like you have
so like:
And in each model you can make a stringvalue called “ObjectId” and have a table with all your ids and set the value to like the object’s name and you can save the object name, and use the name as a way of identifying the object.

data = {
   soda = Soda.Value;
   price = Price.Value;
   objectId = Model.ObjectId.Value;
} 
local success, errormessage = pcall(function()
		DataStore:SetAsync(PlayerId, data)
	end)

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