I always test my code before posting, and all the
above codes works in my testlevel, maybe there are
problems triggered some where else in your script.
For example, the "target.x" in your code could be
a problem.

Maybe post your whole script here, or better upload
your testlevel + script, then we will know your problem
more clearly.