I have a system that saves leaderstats and is supposed to save bool values for unlocked guns (this is a modified script from another dev forum post)
I’ve tried debugging with prints, testing internally by adding boolvalue but nothing seem’s to work and I can’t figure out what the issue with the code is
the money is saved but the bool values do not save, and there is no result when printed in the for loops
local dss = game:GetService("DataStoreService")
local dataStore = dss:GetDataStore("testing4")
game.Players.PlayerAdded:Connect(function(player)
local unlocked = Instance.new("Folder")
unlocked.Name = "unlocked"
unlocked.Parent = player
local loadData = {}
local success, fail = pcall(function()
loadData = dataStore:GetAsync(player.UserId .. "-data") -- use the right data key name
end)
if success then
player.leaderstats.money.Value = loadData[1]
for i, v in pairs(loadData[2]) do
local newweapon = Instance.new("BoolValue")
newweapon.Name = v
newweapon.Parent = unlocked
end
else
warn("Error loading for " .. player.Name .. ". " .. fail)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local saveData = {}
local unlocked = {}
for i, v in pairs(player.unlocked:GetChildren()) do
table.insert(unlocked, v.Name) -- add the gun names to the gunsUnlocked table
end
table.insert(saveData, player.leaderstats.money.Value) -- insert the values youre saving into the main save table
table.insert(saveData, unlocked) -- saving will allow tables inside tables
local success, fail = pcall(function()
dataStore:SetAsync(player.UserId .. "-data", saveData) -- use the right data key name
end)
if success then
print(player.Name .. "'s data saved!")
else
warn("error saving for " .. player.Name .. ". " .. fail)
end
end)