Huge Lag Spike When Deleting Objects

I want the player to be able to sell their inventory. However, when selling a ton of items at once, The game lags like CRAP.
Here’s my script:

script.Parent.Triggered:Connect(function(plr)
	local counter = 0
	for i, v in pairs(plr.Backpack:GetChildren()) do
		counter += 1
		if v:FindFirstChild("value") then
			local value = v.value.Value
			v:Destroy()
			plr.leaderstats.Cash.Value += value
		end
		if counter == 25 then
			counter = 0
			wait(0.1)
		end
	end
end)

I thought selling 25 items in bulk and then giving the script a 0.1 second break (to avoid taking literally a billion years to sell everything) would reduce lag, and while it did just a tiny bit, it is still VERY laggy as you can see in this video:

1 Like

Try using the :ClearAllChildren() method on the player’s backpack as this reply to another post states.

1 Like

So this would mean I would have to get the values first, add them up, give the player their money, and THEN get rid of all the children? (this is a question, just confirming)

Check the microprofiler to see what’s causing the lag.

1 Like

It’s definitely spiking when I use the shredder. (sell items)

The microprofiler can tell you exactly what’s causing the lag, you know that a lag spike occurs when you interact with it, but you don’t know what exactly causes it.

Screen Shot 2025-06-02 at 3.42.41 PM
Here’s the result:
The orange and yellow is the script trying to destroy everything in your inventory, finding the value of the tool and changing your leaderstats. This seems like a problem, as it is mainly the destruction that makes up the orange bar.

This didn’t work, it made the lag worse.

Strange, destroying shouldn’t take that long, how complex are your instances?

Add a task,wait() after destroying every object. Another thing you can do is remove it from the player’s inventory so it doesn’t show and delete it in the background gradually with task.wait()

Item with the least possible children:

  • 2 string values
  • 1 number value
  • 1 object value
  • 1 script
  • 3 parts
  • 2 weldconstraints

Item with the most possible children:

  • 11 string values
  • 1 number value
  • 1 object value
  • 1 script
  • 6 parts
  • 5 weldconstraints

This is unfortunately ALL necessary for saving inventories in DataStores the way I’m doing it. This way of saving causes no lag and is actually PRETTY efficient.

That’s a band-aid fix, destroying instances generally shouldn’t be that expensive.

Consider storing that data in a module or something on the server instead of using Instances.

actually, destroying instance is often expensive, especially if you are destroying a lot of them at one time.

I did some benchmarking, destroying 1000 parts took 37 milliseconds. There is something else going on with OP’s issue.

I’m gonna be honest with you, there were probably way more than 1,000 parts in my inventory total.

(i really need to figure out what i want to say before i send it)

In the gif, it probably got cut early, but there were at LEAST a hundred tools in my inventory before selling.

Well that’s probably your issue! Either have “virtual” items that are just tables of data instead of Instances, or throttle destroying them.

…You’re starting to lose me. I’m not exactly a perfect dev, so could you explain a little bit more on what “virtual items” are?