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
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
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.
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.