First of all, why do you put semicolons in the middle of "if" statements? For example in this line:
if (IsMoving = false); {dbStopObject (2);};
The semicolon after the "if" condition closes the statement, so you create an empty "if" statement that doesn't do anything, and after that comes the dbStopObject which will always be executed because it is not connected with the "if" at all. It's the same issue in every line, and you don't need brackets around single statements and semicolons after brackets either.
In the third line, you also mis-typed the "IsMoving" check, because you put a single equation mark instead of double. The result is that IsMoving will be set to false and dbStopObject will always be executed, even without the extra semicolon. The compiler should give you a warning for that. To avoid this error, use the ! (not) operator instead of "==false".
But even after these corrections, the code won't work as intended, because if dbLoopObject is continuously called while the key is held down, then it will always start playing the animation from the beginning, so it will only show the first frame. You need to detect the moment when the key is pressed and when it's released. For example, try something like this:
if ((dbKeyState(17)== 1 && dbKeyState(31) == 0) ||
(dbKeyState(17)== 0 && dbKeyState(31) == 1))
{
if (! IsMoving)
{
dbLoopObject (2, 0, 160);
IsMoving = true;
}
} else {
if (IsMoving)
{
IsMoving = false;
dbStopObject(2);
}
}
Not tested but it should work. Hopefully. Anyway, the idea is to detect when the status of the object changes from moving to stationary and back, and call the looping and stopping functions only at those moments.
(Maybe I would also store the result of the dbKeyState checks in variables, instead of repeated function calls.)