You’re attempting to save a folder to a data store, and that isn’t possible. What you should do is store a dictionary of all the data to be saved.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local HatsDataStore = DataStoreService:GetDataStore("Hats")
Players.PlayerAdded:Connect(function(player)
local hats = Instance.new("Folder", player)
hats.Name = "Hats"
local data = nil
local success, err = pcall(function()
data = HatsDataStore:GetAsync(player.UserId.."-hats")
end)
if success then
if data then -- handle whatever you'd like here and load the existing data
else -- handle what to do when a player has joined for the first time
end
else
warn("There was an error whilst getting hat data!")
warn("Error:", err)
end
end)
Players.PlayerRemoving:Connect(function(player)
local hats = {
-- save a reference to the hat here to be loaded in later
}
local success, err = pcall(function()
HatsDataStore:UpdateAsync(player.UserId.."-hats", function()
return hats -- saves the hat data
end)
end)
if success then
print("Hats saved successfully.")
else
warn("There was an error whilst saving hats!")
warn("Error:", err)
end
end)
The video @Giggio450BR linked at Can't save something to Datastore - #6 by StraightFromCNPP would help you to learn how to save stuff that aren’t values. StringValues, IntValues, etc…