Hello everyone, hope you guys have a good day, I’ve made a code that saves my boolValue’s that is in a folder that is inside a player, script works fine and everything is working, one thing though is when I add a new boolValue, the data is deleted.
Here’s my code:
local players=game:GetService("Players")
local datastore = game:GetService("DataStoreService")
local saving = datastore:GetDataStore("TrophiesSaving")
local Saving = {}
local HTTPService = game:GetService("HttpService")
players.PlayerAdded:Connect(function(p)
if p:FindFirstChild("TrophiesFolder") then
print("Found folder")
local newData = saving:GetAsync(p.UserId)
local data
local success,errorr = pcall(function()
data = HTTPService:JSONDecode(newData)
end)
if success then
print(data)
for i,v in pairs(p:FindFirstChild("TrophiesFolder"):GetDescendants()) do
if v:IsA("BoolValue") then
v.Value = data[i]
print(data)
end
end
else
print(errorr)
end
end
end)
players.PlayerRemoving:Connect(function(p)
if p:FindFirstChild("TrophiesFolder") then
for i,v in pairs(p:FindFirstChild("TrophiesFolder"):GetDescendants()) do
if v:IsA("BoolValue") then
table.insert(Saving, v.Value)
end
end
print(Saving)
local Encoded = HTTPService:JSONEncode(Saving)
saving:SetAsync(p.UserId, Encoded)
end
end)
Here’s the folder located:
Any help is appreciated!