Changing part properties using a script causes lag spikes

Scripts that change a parts properties, like color, or destroy a part, cause the game to lag. I want to know more as to why this is.

I’ve noticed that whenever a serverside script changes a parts property or destroys a part, the game lags for a second. According to the performance tab, the heartbeat slows down. I experimented and found that this has something to do with the amount of parts in the project, as removing projects or enabling streaming improves performance. I tried making a part that constantly changes color in a while loop, and even if i increased the wait time every loop it still froze up for a second.

I have optimized the game by enabling streaming and making areas unload when no players are present. But I would like to know more about what causes the lag and other ways I might be able to optimize it.

I know this question is kinda vague, so feel free to ask me any questions and I will do my best to answer. Thanks.

Have you given it a try on other devices and seen similar result?

That’s a good idea, I’ll try it out. Thanks for the fast response lol

Edit: I tried joining the game on my phone and to my surprise it was much more performant. To add more context, the game runs smoothly on my laptop overall, these lag spikes are the only exception.