so Im trying to create a simple way to make leaderstats and values in the player and saving them both but the values arent saving can someone help me?
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local leaderstatsTable = {
["Steps"] = 0,
["Money"] = 0,
}
local valuesTable = {
["LuckUpgrades"] = 5,
["MaxLuckUpgrades"] = 10,
}
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player
local data
local success, err = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
for name, value in pairs(leaderstatsTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = leaderstats
if success and data and data[name] then
newValue.Value = data[name]
end
end
for name, value in pairs(valuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = valuesFolder
if success and data and data[name] then
newValue.Value = data[name]
end
end
end)
function leaderstatsToTable(leaderstatsTable)
local tbl = {}
for _, intValue in pairs(leaderstatsTable:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
function valuesToTable(valuesFolder)
local tbl = {}
for _, intValue in pairs(valuesFolder:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
players.PlayerRemoving:Connect(function(player)
local leaderstatsFolder = player:FindFirstChild("leaderstats")
local valuesFolder = player:FindFirstChild("Values")
if valuesFolder and leaderstatsFolder then
local dataToSave = valuesToTable(valuesFolder) and leaderstatsToTable(leaderstatsFolder)
local success, err
local attempt = 1
repeat
success, err = pcall(function()
myDataStore:SetAsync(player.UserId, dataToSave)
end)
attempt += 1
task.wait(2)
until success or attempt > 4
if success then
print(player.Name .. "'s data was success fully saved")
else
print(player.Name .. "'s data failed to save")
end
end
end)
game:BindToClose(function()
task.wait(2)
end)
Alright, I’ve figured out the issue. You’re not actually combining the to data tables for values and leaderstats. What this line of code does:
local dataToSave = valuesToFolder() and leaderstatsToFolder()
does is only return only one datatype if the other one exists. What you need to do is merge the two data tables by making another function:
local function dataToTable(data1,data2) -- data1 and data2 are the valuesToTable and leaderstatsToTable
local data = {}
for i,v in pairs(data1) do
data[i] = v
end
for i,v in pairs(data2) do
data[i] = v
end
return data
end
This worked like a charm for me. Tell me if you don’t understand something and I’ll try to clear it up.
I have no idea how to put it in the script can you please tell me how this doesnt work
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local leaderstatsTable = {
["Steps"] = 0,
["Money"] = 0,
}
local valuesTable = {
["LuckUpgrades"] = 5,
["MaxLuckUpgrades"] = 10,
}
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local valuesFolder = Instance.new("Folder")
valuesFolder.Name = "Values"
valuesFolder.Parent = player
local data
local success, err = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
for name, value in pairs(leaderstatsTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = leaderstats
if success and data and data[name] then
newValue.Value = data[name]
end
end
for name, value in pairs(valuesTable) do
local newValue = Instance.new("IntValue")
newValue.Name = name
newValue.Value = value
newValue.Parent = valuesFolder
if success and data and data[name] then
newValue.Value = data[name]
end
end
end)
function leaderstatsToTable(leaderstatsTable)
local tbl = {}
for _, intValue in pairs(leaderstatsTable:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
function valuesToTable(valuesFolder)
local tbl = {}
for _, intValue in pairs(valuesFolder:GetChildren()) do
tbl[intValue.Name] = intValue.Value
end
return tbl
end
players.PlayerRemoving:Connect(function(player)
local leaderstatsFolder = player:FindFirstChild("leaderstats")
local valuesFolder = player:FindFirstChild("Values")
if valuesFolder and leaderstatsFolder then
local function dataToSave(data1,data2) -- data1 and data2 are the valuesToTable and leaderstatsToTable
local data = {}
for i,v in pairs(data1) do
data[i] = v
end
for i,v in pairs(data2) do
data[i] = v
end
return data
end
local success, err
local attempt = 1
repeat
success, err = pcall(function()
myDataStore:SetAsync(player.UserId, dataToSave)
end)
attempt += 1
task.wait(2)
until success or attempt > 4
if success then
print(player.Name .. "'s data was success fully saved")
else
print(player.Name .. "'s data failed to save")
end
end
end)
game:BindToClose(function()
task.wait(5)
end)
try not to copy it and try to figure it out yourself too. But that’s the gist of it. Come back to me if you need more help and I’ll provide the place file with what I used.