Even if it is for a single small script, please, try to include a basic demostrating mission with it.
And now about the script itself. You may try to use a Sleep 1 or even Sleep 2 (or ~2) when checking for height, and once the condition is met, use a much faster loop. A chopper cannot dive too fast, so a slow loop with be much better to detect the < height condition, while you need a quite fast loop to initiate the climb avoidance. This way your script would be much more effective performance wise. Also take into consideration locality of the unit, as setVelocity will work only on local units.
Here is a sqf example of that:
/*
Usage: [ _veh, 40 ]execVM "KeepHeight.sqf"
Where _veh is the helicopter you want to not crash
Second parameter is minimum allowed height over ground
N.B: Increase _height variable if helicopter still crashes, which may still happen
over mountainous terrain.
*/
private["_veh", "_height"];
_veh = _this select 0;
_height = _this select 1;
while {(canmove _veh) && !(isPlayer (driver _veh))} do
{
if ((getPos _veh select 2) > _height) then
{
Sleep 1;
}
else
{
if (local _veh) then
{
_veh setvelocity [ velocity _veh select 0 , velocity _veh select 1 , 1 ];
};
Sleep 0.01;
};
};