Save Folder with Datastores

I have a folder in my game, that contains string values for the player. How would i save these using a datastore? I’ve tried looking everywhere for an answer, but I can’t find any. Thanks!

this isnt really efficient but you could:

  1. create a table.
  2. for each StringValue, assign an index in the table to the value of the StringValue.
  3. use HttpService to JSONEncode the table that you have now.
  4. store that in datastore.

to get the data back, just JSONDecode it & it will spit out the table that you created & encoded.

1 Like

Loop through all the StringValues and combine them into a dictionary:

local Folder = ...;

local SaveData = {};

for _,StringValue in pairs(Folder:GetChildren()) do
    if (StringValue:IsA('StringValue')) then
        SaveData[StringValue.Name] = StringValue.Value;
    end
end

Save using SetAsync/UpdateAsync and load using GetAsync:

1 Like

To save a folder with string values for a player in a Roblox DataStore, you just need to go through the folder’s contents, grab the string values, and save them to the DataStore. Then, when you’re loading the data, you can recreate those string values in the folder. Here’s how to do it:

Saving the folders string values:

local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerDataStore")

game.Players.PlayerRemoving:Connect(function(player)
    local folder = player:FindFirstChild("YourFolderName") -- Replace with your folder's name
    if folder then
        local dataToSave = {}
        for _, stringValue in pairs(folder:GetChildren()) do
            if stringValue:IsA("StringValue") then
                dataToSave[stringValue.Name] = stringValue.Value
            end
        end
        -- Save the data to the DataStore
        local success, err = pcall(function()
            playerDataStore:SetAsync(player.UserId, dataToSave)
        end)
        if not success then
            warn("Failed to save data for player " .. player.Name .. ": " .. err)
        end
    end
end)

Loading the folders string values:

game.Players.PlayerAdded:Connect(function(player)
    local folder = Instance.new("Folder")
    folder.Name = "YourFolderName" -- Replace with your folder's name
    folder.Parent = player

    local success, data = pcall(function()
        return playerDataStore:GetAsync(player.UserId)
    end)
    if success and data then
        for name, value in pairs(data) do
            local stringValue = Instance.new("StringValue")
            stringValue.Name = name
            stringValue.Value = value
            stringValue.Parent = folder
        end
    elseif not success then
        warn("Failed to load data for player " .. player.Name .. ": " .. data)
    end
end)

Hope this helps.

2 Likes

i love how the 3 replies above this are just:

  1. the idea
  2. the implementation
  3. the full implementation
3 Likes

Thanks for the help! This works perfectly

1 Like

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