[pls help] Best way to save data/values from folders using DataStore2?

I am using datastore2 for the first time and I want to save data from a folder, is it right or wrong? As I understand it, am I creating many “DataStore2.Combine” or am I wrong?

local DataStore2 = require(Modules:WaitForChild("DataStore2"))

-- Function to configure values and listen for changes
	local function setupStatValue(statValue)
		local dataStoreKey = statValue.Name
		print(dataStoreKey)
		local valueData = {
			Name = statValue.Name,
			Value = DataStore2(dataStoreKey, Player):Get() or statValue.Value
		}

		local dataStore = DataStore2(dataStoreKey, Player)

		statValue.Value = valueData.Value
		statValue.Changed:Connect(function(NewValue)
			valueData.Value = NewValue
			dataStore:Set(NewValue)
		end)
	end

-- Configure settings and listen for changes for all folders and settings
	for _, folder in ipairs(CEditor:GetChildren()) do
		if folder:IsA("Folder") then
			for _, statValue in ipairs(folder:GetChildren()) do
				if statValue:IsA("StringValue") or statValue:IsA("IntValue") or ("NumberValue") then
					setupStatValue(statValue)
				end
			end
		end
	end

Try this:

local DataStore2 = require(Modules:WaitForChild("DataStore2"))

-- Function to configure values and listen for changes
local function setupStatValue(statValue)
    local dataStoreKey = statValue.Name
    local valueData = {
        Name = statValue.Name,
        Value = DataStore2(dataStoreKey, Player):Get() or statValue.Value
    }

    local dataStore = DataStore2(dataStoreKey, Player)

    statValue.Value = valueData.Value
    statValue.Changed:Connect(function(NewValue)
        valueData.Value = NewValue
        dataStore:Set(NewValue)
    end)
end

-- Configure settings and listen for changes for all folders and settings
for _, folder in ipairs(CEditor:GetChildren()) do
    if folder:IsA("Folder") then
        for _, statValue in ipairs(folder:GetChildren()) do
            if (statValue:IsA("StringValue") or statValue:IsA("IntValue") or statValue:IsA("NumberValue")) then -- Corrected this line
                setupStatValue(statValue)
            end
        end
    end
end

Does the code work better? I want to improve my datastore to the maximum

This one will:

local DataStore2 = require(Modules:WaitForChild("DataStore2"))

-- Initialize Player object (replace 'Player' with actual definition)
local Player = game.Players.LocalPlayer or game:GetService("Players").FirstPlayer

-- Initialize DataStore2 for each stat
local dataStores = {}
for _, folder in ipairs(CEditor:GetChildren()) do
    if folder:IsA("Folder") then
        for _, statValue in ipairs(folder:GetChildren()) do
            if statValue:IsA("StringValue") or statValue:IsA("IntValue") or statValue:IsA("NumberValue") then
                local dataStoreKey = statValue.Name
                dataStores[dataStoreKey] = DataStore2(dataStoreKey, Player)
            end
        end
    end
end

-- Function to configure values and listen for changes
local function setupStatValue(statValue)
    local dataStoreKey = statValue.Name
    local dataStore = dataStores[dataStoreKey]
    if dataStore then
        local valueData = {
            Name = statValue.Name,
            Value = dataStore:Get() or statValue.Value
        }

        statValue.Value = valueData.Value
        statValue.Changed:Connect(function(NewValue)
            valueData.Value = NewValue
            dataStore:Set(NewValue)
        end)
    else
        warn("DataStore2 not initialized for stat: " .. dataStoreKey)
    end
end

-- Configure settings and listen for changes for all folders and settings
for _, folder in ipairs(CEditor:GetChildren()) do
    if folder:IsA("Folder") then
        for _, statValue in ipairs(folder:GetChildren()) do
            if statValue:IsA("StringValue") or statValue:IsA("IntValue") or statValue:IsA("NumberValue") then
                setupStatValue(statValue)
            end
        end
    end
end

Then I really wouldn’t use DataStore2 to begin with. DataStore2 has many features that have already been added by Roblox themselves, and the features it misses like session-locking are crucial to prevent duping and cloning of data.

Some good alternatives:
Suphi's DataStore Module (I prefer this one)
Save your player data with ProfileService! (DataStore Module)

Why are you referencing the LocalPlayer on a server script? This won’t work. .FirstPlayer also doesn’t exist.

1 Like

With ProfileService can I save data and folders easier?

It’s a bit harder to use, but it guarantees there’s no data loss or duplication.

Suphi’s DataStore Module is easier to use than ProfileService, and is also better regarding performance, which is why I prefer it. He also has a tutorial on how to use it, and it goes over everything.