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)