Any way to cleanup this datastore?

NOTE: Data randomly decides to save at some point and not save at other points

local data = game:GetService("DataStoreService"):GetDataStore("insertdatastorehere")


game:GetService("Players").PlayerAdded:Connect(function(plr)
	local Inventory = Instance.new("Folder", plr)
	Inventory.Name = "Inventory"
	
	local squad = Instance.new("Folder", Inventory)
	squad.Name = "Squad"
	
	for i=1,3 do
		
		local character = Instance.new("StringValue", squad)
		character.Name = "Hero"..i
		character.Value = "N/A"
		
		local level = Instance.new("NumberValue", character)
		level.Name = "Level"
		level.Value=1
		
		local stars = Instance.new("NumberValue", character)
		stars.Name = "Stars"
		stars.Value=1
		
		local Mastery = Instance.new("NumberValue", character)
		Mastery.Name = "Mastery"
		Mastery.Value=0
		
		local icon = Instance.new("Decal", character)
		icon.Name = "Icon"
		icon.Texture = 0
		
	end
	local getData
	local success, err = pcall(function()
		getData = data:GetAsync(plr.UserId)
	end)

	if success then
		if getData then

			Inventory.Squad.Hero1.Value = getData[1]
			Inventory.Squad.Hero2.Value = getData[2]
			Inventory.Squad.Hero3.Value = getData[3]
			Inventory.Squad.Hero1.Level.Value = getData[4]
			Inventory.Squad.Hero2.Level.Value = getData[5]
			Inventory.Squad.Hero3.Level.Value = getData[6]
			Inventory.Squad.Hero1.Stars.Value = getData[7]
			Inventory.Squad.Hero2.Stars.Value = getData[8]
			Inventory.Squad.Hero3.Stars.Value = getData[9]
			Inventory.Squad.Hero1.Mastery.Value = getData[10]
			Inventory.Squad.Hero2.Mastery.Value = getData[11]
			Inventory.Squad.Hero3.Mastery.Value = getData[12]
		else
			
			for i, v in pairs(Inventory.Squad:GetChildren()) do
				v.Value = "N/A"
				v.Level.Value = 1
				v.Stars.Value = 1
				v.Mastery.Value = 0
			end
			
		end
	else
		warn(err)
	end
end)

function saves(Player)

	local saveData = {}

	for i, v in pairs(Player.Inventory:GetChildren())do
		saveData[#saveData + 1] = v.Name
	end

	data:SetAsync(Player.UserId, saveData)

end
game:BindToClose(function()
	for _, v in pairs(game.Players:GetChildren())do
		saves(v)
	end
	
end)

game:GetService("Players").PlayerRemoving:Connect(function(plr)
	
	local save = {}
	
	
		
	table.insert(save, plr.Inventory.Squad.Hero1.Value)
	table.insert(save, plr.Inventory.Squad.Hero2.Value)
	table.insert(save, plr.Inventory.Squad.Hero3.Value)
	table.insert(save, plr.Inventory.Squad.Hero1.Level.Value)
	table.insert(save, plr.Inventory.Squad.Hero2.Level.Value)
	table.insert(save, plr.Inventory.Squad.Hero3.Level.Value)
	table.insert(save, plr.Inventory.Squad.Hero1.Stars.Value)
	table.insert(save, plr.Inventory.Squad.Hero2.Stars.Value)
	table.insert(save, plr.Inventory.Squad.Hero3.Stars.Value)
	table.insert(save, plr.Inventory.Squad.Hero1.Mastery.Value)
	table.insert(save, plr.Inventory.Squad.Hero2.Mastery.Value)
	table.insert(save, plr.Inventory.Squad.Hero3.Mastery.Value)

	local success, err = pcall(function()

		data:SetAsync(plr.UserId,save)

	end)

	if success then
		print("Success!")
	else
		warn(err)
	end
	saves(plr)
end)

You’re already using a loop to create the values, so why not add them to the table using a loop too? Your saves function might also be overriding the saved data.