Also, the server freezes for 5 seconds when using that snippet
If you have a lot of objects then looping them all may take some time. Codes above are valid, message about invalid data is probably caused by trying to get non-exist object's world, so before getting world check if object is valid.