Destroying NPC causes lag spike

I have several NPCs, when I destroy the first one it causes a lag spike, but the rest are fine. If I remove the humanoid and just have an empty rig, it doesn’t cause that weird lag spike. But for my animations to work, I require the humanoid. Is there any work around this?

SellPart.ProximityPrompt.Triggered:Connect(function(player)
	local char = game.Workspace:FindFirstChild(player.Name)
	if not char then return end

	local HumanoidRootPart = char:FindFirstChild("HumanoidRootPart")
	if not HumanoidRootPart then return end

	local WeldNPC = HumanoidRootPart:FindFirstChild("WeldNPC")
	if not WeldNPC then return end

	for _, v in pairs(game.Workspace.Noobs:GetChildren()) do
		local StatsFolder = v:FindFirstChild("Stats")
		if StatsFolder then
			local BeingCarried = StatsFolder:FindFirstChild("BeingCarried")
			local WhoIsCarrying = StatsFolder:FindFirstChild("WhoIsCarrying")
			if WhoIsCarrying and BeingCarried and WhoIsCarrying.Value == player.Name and BeingCarried.Value == true then
				v:Destroy()
				WeldNPC:Destroy()
				break -- Stop loop after finding the NPC
			end
		end
	end
end)
2 Likes

im not positive this will help, but try deleting the humanoid first?

3 Likes

Use the microprofiler to check exactly what’s causing the lag spike.

2 Likes

Bumping this topic! I’ve been experiencing this issue aswell. The first NPC to be deleted will cause a huge lag spike. I was wondering if @Virvek has found a solution to this issue or someone else who could find this topic. It seems that it’s a roblox bug but I can’t confirm this..

1 Like

It’s a studio bug I’ve had it happen as well before, happens with basically any Instance you destroy first

1 Like

You can use an AnimationController, you can run animations via it on non humanoid rigs.