Scotland succumbed to defeat against world champions South Africa despite Stuart Hogg becoming his country's joint-top try scorer.
The Scots defied a heavy penalty count to lead 10-8 at the break after Hogg replied to Makazole Mapimpi's score.
Mapimpi crossed again and more ill discipline allowed Elton Jantjies to kick the tourists further clear.
Hogg got his 24th Test try before Handre Pollard and Frans Steyn kicked further penalties for the Springboks.
Finn Russell failed to convert one of the Hogg tries and also missed a penalty.
British and Irish Lions full-back Hogg has now scored as many Scotland tries as Ian Smith and Tony Stanger.
Gregor Townsend's side conclude their series at home to Japan next weekend, while South Africa take on England.
With five consecutive wins at their backs, Scotland were in confident mood. Last weekend's win over Australia was their third win against a world top-three side this year.
The Springboks are a different animal to England or France or Australia, though. World champions and Lions tamers, this would be the ultimate test of Scotland's claim that they belong among the truly elite.
There was a lovely moment when Tom Smith, the Scotland and Lions great, presented the match ball alongside his family who have supported him through his battle with cancer. The Murrayfield crowd afforded their hero a rapturous reception.
The din soon subsided when the action started as the men in green set to work. First scrum? Penalty South Africa. And the penalties kept coming as the Scots struggled to repel wave after wave of thunderous Boks runners.
But repel it they did, and Duhan van der Merwe's searing break from deep in his own half took Scotland into opposition territory for the first time. They earned a penalty of their own and, incredibly after an early onslaught, the Scots led 3-0 through Russell.
Jantjies soon wiped that out and just before the half-hour the Boks struck with the game's opening try.
There seemed little danger as the ball was shipped through the South Africa backline, but Rufus McLean hesitated for a split-second in deciding which attacker he should take and that was enough for Mapimpi, that man who always seems to be in the right place at the right time, to streak up the touchline to score.
The general consensus was if Scotland were to spring a shock here, Russell would need to be at his brilliant best. The Racing fly-half sparked his side into life with a pinpoint cross-field kick to Van der Merwe that opened up the Boks defence. Superb handling from Hogg, Nick Haining and Chris Harris teed-up Hogg, who eclipsed Rob Kearney's record as Test rugby's most-capped full-back, to dive over. It was a stunning try and Russell's conversion gave the Scots a two-point lead.
It could have been a five-point cushion had Russell put away a fairly straightforward penalty chance right on half-time, and the home support worried whether that miss could come back to bite them.
That fear was reinforced when the visitors reclaimed the lead early in the second half, Mapimpi again making himself the extra man out wide to run in his second try. Jantjies' conversion and a penalty soon after gave the world champions an eight-point lead. Mountain climbing territory for Scotland.
Townsend reached for the biggest weapon at his disposal, throwing on Hamish Watson, but Jantjies struck again from the tee as the Boks started to pull away at 10-21.
The Scots refused to go quietly. Another sweeping move created space for Hogg to race over for his second try of the match.
A six-point game and suddenly anything seemed possible but substitute Pollard punctured any hopes of a famous Scottish win with a penalty either side of a long-range one from Steyn to take the Springboks out of reach.
Scotland are a team on the rise but were reminded they still have a way to go.
Scotland: Hogg (capt), McLean, Harris, Scott, Van der Merwe, Russell, Price; Schoeman, McInally, Z Fagerson, Skinner, Gilchrist, Haining, Ritchie, M Fagerson
Replacements: Ashman, Bhatti, Kebble, Hodgson, Watson, Horne, Hastings, Kinghorn
South Africa: Le Roux, Kriel, Am, de Allende, Mapimpi, E Jantjies, H Jantjies, Nche, Mbonambi, Nyakane, Etzebeth, Mostert, Kolisi, Smith, Vermeulen
Replacements: Marx, Kitshoff, Koch, De Jager, Wiese, Reinach, Pollard, F Steyn
Referee: Angus Gardner (Australia)