what is this script supposed to move? people or inanimate objects?\
the most straightforward way to do it would be to have a global variable that can be flipped to true/false when the script is activated
otherwise, if you are dealing with an inanimate object, you could have the script do a check of whether the object is moving in a straight line, and then exit if it isnt.
another way i just thought of, which would probably be easier, would have the script check if the object is where it should be at the beginning (or end) of the loop (the important thing is that it's after the wait-time). this would only work if the object is inanimate though, and you would need to keep track of a script's age (so you delete the older one and not the younger one)
added stuff is surrounded by ******
_unit = _this select 0
_target = GL
_speed = 5
_2x = 0
_2y = 0
; *****
_ageofscript = 0
; *****
_dir=[_unit,_target] call DirToObj
#loop
_1x= getpos _unit select 0
_1y= getpos _unit select 1
_2x=_1x+(_speed * (sin(_dir)))
_2y=_1y+(_speed * (cos(_dir)))
_unit setpos [_2x, _2y, 0]
; *****
_unitpos = getpos _unit
; *****
~ delayvariable
; *****
?! (getpos _unit == _unitpos) and ? (_ageofscript > 1) : exit
_ageofscript = _ageofscript + 1
; *****
goto "loop"