Issue with saving bool values to player folder with datastore

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 :cry:

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)

I’m really stuck on this, I’ve used a few different prints now, and while when my shop is used the bool value is created, it doesn’t save :cry::cry:any help would be good

You can probably get some code from this resource, it saves leaderstats too:

(If you want to use that resource, you just get the leaderstats folder by doing player:WaitForChild("leaderstats"), which waits for it to load. When it’s added everything from the last session is there. The folder is then automatically saves and stuff.)


Your code looks fine, I’m not seeing any problems with it.

It might be because of how studio closes, normally you also want to add a save function for game:BindToClose that saves all the player’s data. Maybe when a play test closes it closes before firing playerRemoving.