How to reduce lag spike when destroying parts

so im making this redzone effect when it touches anything from buildings to players they disappear instantly, the problem is it causes lag spike everytime it touches the part to destroy

local t = game:GetService("TweenService")
local a = TweenInfo.new(2,Enum.EasingStyle.Linear)
local c = script.Parent
local v = Vector3.new(200,200,200)
c.Touched:Connect(function(p)
	pcall(function() -- first time i used pcall and yes if awarding the badge gives errors then the script should ignore this function in the pcall and continue on
		if (p.Parent:FindFirstChild("Humanoid")) then
			local pa = game.Players:GetPlayerFromCharacter(p.Parent)
			if pa then
				game:GetService("BadgeService"):BadgeAward(p.UserId,2124619174)
			end
		end
	end)
	for _, i in pairs(c:GetTouchingParts()) do -- loop thru all the parts that are touched by the redzone part
		if (i:IsA("Part") and i.CanCollide == true and i.Anchored == false) then
			i:Destroy() -- or remove()??? I dont see the difference between the two
		end
	end
end)
if c.Parent == workspace then
	local t = t:Create(c,a,{Size = v; Transparency = 1})
	t:Play()
	t.Completed:Wait()
	c:Destroy()
end

any help?

1 Like

I am not sure what causes the lag without seeing a gif for something, but I think it might have to do with looping through all the touched every time .touched is fired, so maybe don’t loop through everything and only destroy the new part that it touched if it fulfills your arguments, then destroy it.

I don’t quite understand but i dont know another way of checking the parts with get touching parts Without a loop and sorry I cant upload any gifs or vids, just pictures due to bad internet

c.Touched:Connect(function(p)
	pcall(function() -- first time i used pcall and yes if awarding the badge gives errors then the script should ignore this function in the pcall and continue on
		if (p.Parent:FindFirstChild("Humanoid")) then
			local pa = game.Players:GetPlayerFromCharacter(p.Parent)
			if pa then
				game:GetService("BadgeService"):BadgeAward(p.UserId,2124619174)
			end
		end
	end)
	if (p:IsA("Part") and p.CanCollide == true and p.Anchored == false) then
		p:Destroy() -- or remove()??? I dont see the difference between the two
	end
end)

see how instead of looping through all the parts it is touching, just destroy p, the new part it started touching. (not sure if this will help)

p.s. you are correct to use :Destroy(), :Remove() is deprecated.

2 Likes

Hello hello, have you thought about using game.Debris:AddItem(), it may help in performance but sadly I do not think there is a way to reduce lag while destroying a mass amount of parts unless you somehow cache them.

2 Likes

never heard of it, how do you “additem”?? Nevermind,
anyway yeah it kinda increase performance the lag spikes reduced a bit but thats probably the only way :confused: but thanks very much!

I’m still gonna answer your question haha, even though you might know it, I’ll still say it…

You quite literally just type in game.Debris:AddItem(TheInstance,2) the number is the amount of time you would like to wait before it is destroy and the instance is self explanatory.
Anyways, I think saying all this was pointless but I said it anyways.

1 Like

pls correct if im wrong what i stated below v v v

Summary

Just learnt for over a week and i wished i had thought of it earlier but i just wanna say that, i thought of using in ipairs instead of in pairs and the result reduce the lag significantly, i dont know how maybe because it iterates through the first child of the Gettouchingparts and then work out to the last part but by using pairs (i think?) it just gets all the touching parts instantly randomly and cause a lag spike, i had to say this bcus probs just fit as a little PSA :grin: sorry to bump in but anyway have a nice day! :grinning:

Alright, well good for you. I’ll make sure to note that for myself in the future too just incase!

1 Like