How would you save everything in a folder to a datastore?

I’m trying to create an events calendar that certain ranks in the group can add to. I’m trying to save everything in the “EventsFolder” folder when the person leaves the game and when someone joins the game I’m trying to populate the folder with every event. I’ve included the code that I’ve tried which I tested by manually adding a string value to the folder then rejoining the game but the value didn’t appear again when I joined. Thanks for the help!

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“PointsDataStore”)

game.Players.PlayerAdded:Connect(function(player)

local eventFolder = Instance.new(“Folder”)
eventFolder.Name = “EventsFolder”
eventFolder.Parent = ReplicatedStorage

local dataEvents

local success, errormessage = pcall (function()
dataEvents = myDataStore:GetAsync(player.UserId…“-events”)
end)

if success then
for i = 1, #dataEvents do
dataEvents[i].Parent = ReplicatedStorage:FindFirstChild(“EventsFolder”)
end
else
print(“Error while attempting to recieve player data.”)
warn(errormessage)
end

end)

game.Players.PlayerRemoving:Connect(function(player)

local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId…“-events”,ReplicatedStorage.EventsFolder:GetChildren())
end)

if success then
print(“Player data successfully saved!”)
else
print(“There was an error while attempting to save player data.”)
warn(errormessage)
end

end)

Sorry this code is a mess, I can’t find a code block format preset :upside_down_face:

For the formatting use ``` on the top and bottom of your entire code.

Unfortunately, you cannot store objects inside of datastores. You’ll need to break it down into properties and their values. Then, once they join, you recreate the object from the properties and their values. It’s like getting the information about every single atom of an object and using the data to recreate the object from scratch (I know that’s a weird analogy).

This is actually called serialization, check out @starmaq’s tutorial on this:

It’s quite a whole new topic, so that’s the best I can explain. That topic is your best chance!

1 Like

I use tables to save things like that, you’d set the objects name into the table and when they join loop t through the table and do whatever from there.
Here is an example of my tool saving:

local DSS = game:GetService("DataStoreService")
local SwordsDS = DSS:GetDataStore("SwordsDataStore")
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local GlobalWeaponsTable = {}
game.Players.PlayerAdded:Connect(function(Player)
	local LocalWeaponsTable = table.insert(GlobalWeaponsTable, Player.UserId)
	local WTData
	local SheatheredKatanaData
	local PlayerIdFolder = Instance.new("Folder")
	PlayerIdFolder.Parent = RS
	PlayerIdFolder.Name = Player.UserId



Player.CharacterAdded:Connect(function(Character)
	local Backpack = Player.Backpack
	
Backpack.ChildAdded:Connect(function(Child)
	if SS.Weapons.Swords:FindFirstChild(Child.Name) and not table.find(LocalWeaponsTable, Child.Name) then
		if not table.find(LocalWeaponsTable, Child.Name) then
		table.insert(LocalWeaponsTable, Child.Name)
	end
	end
	
	
end)
end)
	
end)

Of course this is not the whole script, I left out a lot of it but you can learn a bit or 2 reading it.
Also, the format for lua is,

```lua
–code goes here
```

PS: This probably isn’t the most efficient way but it works.

Wrote a post on this before.