While wait() loop destroys or can’t detect children

I’m trying to rotate a model inside a viewport frame. It works for one part of the script but doesn’t work for the other part even though they are pretty much the same.

I noticed when the script enters the while do loop it destroys all children.


This is the code that works.


This is the code that doesn’t work. (Pretty similar right?)

This is the coroutine function. It works for the first image but doesn’t work for the second image. It destroys the children for when calling the function from the second image. (Not really destroys it just can’t detect the children. Maybe it is being destroyed it’s hard to tell.)

I am so confused right now so any help will be appreciated!

Instead of using coroutines, have you tried setting a value to false and only having the loop run while the value is true?

The thing is it’s an inventory system so multiple objects will rotate and this function will run many times. I could use the value thing that you said but just still use the coroutine.

That was my fault as I should’ve said it being an inventory system.

UPDATE: I just made a mistake. After I called the function I deleted all viewport frames for some reason. That’s why it bugged out.