The file you posted does raise an error at line 19 since you left target as a list instead of a game object. You need to set target to an individual object from the list before you use target.worldOrientation.
Thanks blenderer2012, my file was behaving odd as a result of the Euler rotation, I changed it to quaternion and it behaved better.
Euler has this odd hate for 360 degree rotations, it can’t seem to interpret how the object should reach a particular point in rotation.
I needed to copy only one axis because I need the other axis (axises? axies?) to be influenced by other things.
So what I have now is this:
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner
# objects with property
objects_with_prop = [o for o in scene.objects if "CopyMe" in o]
target = objects_with_prop
dist = lambda o: (o.worldPosition - own.worldPosition).magnitude
object_distance = [(o, dist(o)) for o in objects_with_prop]
object_distance.sort(key=lambda t: t)
closest = object_distance
target = closest
# copy rotation
eu_own = own.worldOrientation.to_quaternion()
eu_target = target.worldOrientation.to_quaternion()
eu_own.z = eu_target.z
eu_own.w = eu_target.w
own.worldOrientation = eu_own
Euler has been such a headache because it doesn’t do 360 degrees (some years ago, I screamed at my monitor in rage as I attempted to make my character spin)
Knowing this now, I don’t use Euler because it produces odd results like you saw in my blend, you guys gave me the code and I simply fit it to my needs (I didn’t think to post my corrected blend lol)