Why is my DataStore so buggy

Hello fellow developers -

In all my time as a developer, I’ve never really used DataStores. So I’m learning now.
I want to save 10 Boolean values to the player in a specific folder, each bool being an indicator to a different overhead title that other scripts will reference. I have put this excuse of a script together based of my knowledge and guidance from tutorials and such.

Whenever the game launches, not all of the booleans are even inserted into the folder, and most of the time the pcall fails. I believe it is because of the sheer amount of Data I am trying to save, though like I said before, I’m not sure.

Here is my excuse of a script:



local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") 

local function saveData(player)

	local tableToSave = {
		player.TitleBools.FAN.Value, 
		player.TitleBools.DIAMONDMEMBER.Value,
		player.TitleBools.ELITEMEMBER.Value,
		player.TitleBools.GOLDENMEMBER.Value,
		player.TitleBools.HEROICMEMBER.Value,
		player.TitleBools.LEGEND.Value,
		player.TitleBools.LEGENDARYMEMBER.Value,
		player.TitleBools.THEBEST.Value,
		player.TitleBools.VALUEDMEMBER.Value,
		player.TitleBools.CHAMPION.Value
		
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) 
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player) 


	local Folder = Instance.new("Folder")
	Folder.Name = "TitleBools"
	Folder.Parent = player

	local fan = Instance.new("BoolValue")
	fan.Name = "FAN"
	fan.Parent = Folder

	local diamondmember = Instance.new("BoolValue")
	diamondmember.Name = "DIAMONDMEMBER"
	diamondmember.Parent = Folder
	
	local elitemember = Instance.new("BoolValue")
	elitemember.Name = "ELITEMEMBER"
	elitemember.Parent = Folder
	
	local goldenmember = Instance.new("BoolValue")
	goldenmember.Name = "GOLDENMEMBER"
	goldenmember.Parent = Folder
	
	local heroicmember = Instance.new("BoolValue")
	heroicmember.Name = "HEROICMEMBER"
	heroicmember.Parent = Folder

	local legend = Instance.new("BoolValue")
	legend.Name = "LEGEND"
	legend.Parent = Folder	
	
	local legendarymember = Instance.new("BoolValue")
	legend.Name = "LEGENDARYMEMBER"
	legend.Parent = Folder	
	
	local thebest = Instance.new("BoolValue")
	legend.Name = "THEBEST"
	legend.Parent = Folder	
	
	local valuedmember = Instance.new("BoolValue")
	legend.Name = "VALUEDMEMBER"
	legend.Parent = Folder	
	
	local champion = Instance.new("BoolValue")
	legend.Name = "CHAMPION"
	legend.Parent = Folder	
	

	local data
	local success, err = pcall(function()

		data = dataStore:GetAsync(player.UserId) 

	end)

	if success and data then 

		fan.Value = data[1] 
		diamondmember.Value = data[2] 
		elitemember.Value = data[3]
		goldenmember.Value = data[4]
		heroicmember.Value = data[5]
		legend.Value = data[6]
		legendarymember.Value = data[7]
		thebest.Value = data[8]
		valuedmember.Value = data[9]
		champion.Value = data[10]
	else 
		print("The player has no data!") 
	end

end)

game.Players.PlayerRemoving:Connect(function(player) 
	local success, err  = pcall(function()
		saveData(player) 
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

The first error that I saw was that half of the bools don’t even go into the folder.
only these 6 replicate:
image
And if I enable any of the bools, it doesn’t save.
Help please!

Your code is quite clunky and the reason not all your variables were being inserted is being you were referring to some of them as “legend” when naming them and parenting them. A quick check of your code would’ve easily confirmed this. I created some new and improved code that you can read through to understand.

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") 

local function saveData(player)

	local titles = {}

	for _, title in pairs(player.TitleBools:GetChildren()) do
		titles[title.Name] = title.Value
	end

	local tableToSave = {
		Titles = titles
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) 
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player) 
	local TitleBools = Instance.new("Folder")
	TitleBools.Name = "TitleBools"
	TitleBools.Parent = player

	local fan = Instance.new("BoolValue")
	fan.Name = "FAN"
	fan.Parent = TitleBools

	local diamondmember = Instance.new("BoolValue")
	diamondmember.Name = "DIAMONDMEMBER"
	diamondmember.Parent = TitleBools

	local elitemember = Instance.new("BoolValue")
	elitemember.Name = "ELITEMEMBER"
	elitemember.Parent = TitleBools

	local goldenmember = Instance.new("BoolValue")
	goldenmember.Name = "GOLDENMEMBER"
	goldenmember.Parent = TitleBools

	local heroicmember = Instance.new("BoolValue")
	heroicmember.Name = "HEROICMEMBER"
	heroicmember.Parent = TitleBools

	local legend = Instance.new("BoolValue")
	legend.Name = "LEGEND"
	legend.Parent = TitleBools	

	local legendarymember = Instance.new("BoolValue")
	legendarymember.Name = "LEGENDARYMEMBER"
	legendarymember.Parent = TitleBools	

	local thebest = Instance.new("BoolValue")
	thebest.Name = "THEBEST"
	thebest.Parent = TitleBools	

	local valuedmember = Instance.new("BoolValue")
	valuedmember.Name = "VALUEDMEMBER"
	valuedmember.Parent = TitleBools	

	local champion = Instance.new("BoolValue")
	champion.Name = "CHAMPION"
	champion.Parent = TitleBools

	local data
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId) 
	end)
	
		
	if success and data then 
		print(data)
		for _, title in pairs(TitleBools:GetChildren()) do
			title.Value = if data.Titles[title.Name] then data.Titles[title.Name] else false
		end
	else 
		print("The player has no data!") 
	end

end)

game.Players.PlayerRemoving:Connect(function(player) 
	local success, err  = pcall(function()
		saveData(player) 
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player) 
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)

Ah I can’t believe I missed the duplicate variables. (explains why no errors lol)

Script looks great, but data is not saving. No matter how much I change it in the game it doesn’t save

Could you send me your current data script(as in including everything you’ve changed etc)