Scotland failed to "kick on" in the second half against South Africa but will "be better" for the experience, says head coach Gregor Townsend.
The hosts drew level for a third time, at 20-20, after 46 minutes, after Springboks full-back Willie le Roux was sin-binned at Murrayfield.
But South Africa edged back in front while a man down for 10 minutes, and Scotland botched two late line-outs.
"I am disappointed for the team," Townsend told BBC Sport.
"A lot of effort went into that performance and there were some great moments that got us back in the game, and moments in the second half where we could have - and should have - kicked on. But the effort the players put in was outstanding.
"We are a team that is learning every time we come together. It is a painful loss today, but we will be better for it."
Townsend defended his team's decision to kick for the corner, rather than at goal, on the hour when a penalty would have levelled the scores again at 23-23.
"It is a players' decision," Townsend said. "They stood up and made that decision and obviously felt that was the right thing to do. There was bravery in that but credit to South Africa, they defended the maul.
"It is one of a thousand moments where we will look at it and go, 'Right, what were our options? Could we have done something different?' We would all be smiling if we had scored from the line-out drive, which we had looked strong in.
"But fair play to South Africa. They are one of the biggest and best packs around, and they showed what they were about with their defence of that maul."
Scotland captain Greig Laidlaw said he "thought about kicking the points but the two previous mauls probably pushed us into going for the corner".
"I have always been aggressive in my thinking, I back myself and I back the team, but it's disappointing." he added. "South Africa had another two kicks at goal so we probably would have had to score again to win the game."
'When you play the best teams, you learn more'
Scotland struggled to make headway early on against the Springboks' blitz defence, and were regularly turned over at the breakdown.
But they scored one brilliant try from deep, finished off by Peter Horne, and a second before the interval via Hamish Watson from an inventive line-out routine.
"It was a tight game all the way through and I felt we were the team that was looking to break out," Townsend told BBC Scotland.
"We scored a couple of good tries and played with a lot of energy at the start of that second half. We just didn't kick on - partly due to our errors and partly to the way South Africa controlled the possession.
"They are a really good team and when they get ahead, they are very tough to get the ball off. It is hard to get a contest for possession.
"But when you play the best teams, you learn more. You learn what they are doing that you have maybe never come up against, and how to combat it.
"The players were working that out during the game. We found a lot of space which maybe in the first 10-15 minutes - because of their line speed - we weren't able to.
"But we certainly started to find that more and it's just a pity that we didn't get that amount of possession in the last 20 minutes to keep going for it and get ahead."
'We are going in the right direction' - Laidlaw
Townsend played down the impact of losing Stuart Hogg in the 63rd minute, which he said was "more precautionary" after the full-back received treatment to his left ankle.
"Hogg was excellent but by that stage [when he went off] South Africa had a lot of possession and it was tough to get the ball off them," he added.
Laidlaw believes Scotland are "right up there" among the elite teams in world rugby despite their first Murrayfield defeat since a narrow loss to New Zealand this time last year.
"We showed that we are right in these games; on another day we can win so we are going in the right direction," he added.
"We are disappointed we have lost but we need to put that to the side and bounce back next week and give a strong performance against Argentina."