How to delete things properly without causing lag?

It’s weird, everytime I destroy a model through a script in a for loop or any recursive loop and it has like about 120 parts my frames drop. Anyone experience this? I used the Debris service and that somewhat improved performance even uses a task.spawn() to hopefully utilize more than one thread. I check the FPS drop on the server and it went from 60 to 30 when i did the :Destroy() method

2 Likes

You should just call Model:Destroy() without any loops.
If you want the model to stay, but children cleared - use Model:ClearAllChildren()

2 Likes

Perhaps you’re pressuring the server too much by destroying a lot of parts practically instantaneously?

My guess is to add a task.wait(adjustable) before you actually destroy the part (assuming the loop only does that)

1 Like

so i only call the method :Destroy() on the main parent model. That model has about 20 parts in it, Im not sure if me iterating through it in a for loop would cause lag since there are 70 instances within that model but only 20 of them are actually parts. I delete 10 of these models when i kill them.

Nevermind guys, I fixed it. Here’s the short version of how to resolve this. For models that have lots of parts or instances, make sure to iterate through that model via a for loop or while loop and delete each child/descendent individually via Debris:AddItem(instance, seconds). This prevents huge lag spikes since calling a :Destroy/Debris:AddItem() on just the model itself can put too much of a load on how Roblox handles memory.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.