What is the purpose of the Go To Related Record Step? What layout do you specify in this step?
What layout are you one when you start your first script? (That's important)
This step: Set Variable [$$TotalTimeSpent; Value:if (History::HistoryID > 0 ; Sum(History::TotalTimeSpent))]
doesn't look like right. Since you are on the History layout, Sum ( History::TotalTimeSpent ) will give you the same value as History::TotalTimeSpent. Is TotalTimeSpent a summary field?
Depending on why you need this total, you could compute this total with a relationship and calculations and not need any script at all.
PS. You can avoid the double space effect when posting scripts or other vertical lists if you use Shift-Return instead of return to start a new line.