Hello i tried making a script that saves all the values inside the folder in player by having them in a table and saving thru datastore but it is not saving.
game.Players.PlayerAdded:Connect(function(player)
local folder = player:WaitForChild("CivCars")
local MyDataStore = game:GetService("DataStoreService"):GetDataStore("ValuesSave")
local success,data = pcall(function()
return MyDataStore:GetAsync(player.UserId)
end)
print(data)
if not data then
print("No data found")
end
for i, v in pairs(data) do
for i,ve in pairs(folder:GetDescendants()) do
ve:WaitForChild(i).Value = v
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local MyDataStore = game:GetService("DataStoreService"):GetDataStore("ValuesSave")
local folder = player:WaitForChild("CivCars")
local dictionary = {}
for i,v in pairs(folder:GetDescendants()) do
if v:IsA("BoolValue") or v:IsA("IntValue") or v:IsA("StringValue") then
print(v.Name)
dictionary[v.Name] = v.Value
end
end
local success = pcall(function()
MyDataStore:SetAsync(player.UserId, dictionary)
end)
end)
local players = game:GetService("Players")
local datastores = game:GetService("DataStoreService")
local datastore = datastores:GetDataStore("DataStore")
players.PlayerAdded:Connect(function(player)
local folder = player:WaitForChild("CivCars")
local success, result = pcall(function()
return datastore:GetAsync(player.UserId)
end)
if success then
if result then
if type(result) == "table" then
for name, value in pairs(result) do
local object = folder:FindFirstChild(name, true)
if object then
object.Value = value
end
end
end
end
else
warn(result)
end
end)
players.PlayerRemoving:Connect(function(player)
local folder = player:WaitForChild("CivCars")
local data = {}
for _, child in ipairs(folder:GetDescendants()) do
if child:IsA("ValueBase") then
data[child.Name] = child.Value
end
end
local success, result = pcall(function()
return datastore:SetAsync(player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end)
game:BindToClose(function()
for _, player in ipairs(players:GetPlayers()) do
local folder = player:WaitForChild("CivCars")
local data = {}
for _, child in ipairs(folder:GetDescendants()) do
if child:IsA("ValueBase") then
data[child.Name] = child.Value
end
end
local success, result = pcall(function()
return datastore:SetAsync(player.UserId, data)
end)
if success then
if result then
print(result)
end
else
warn(result)
end
end
end)
Responded to the other thread but I’ll reply here too.
This script worked for me (I created the required folder named “CivCars” and a random StringValue instance which stored my username).