.Touched lag on NPC

I’m currently making an NPC enemy that allows you to slash parts of it to stop it from moving (it’s tendons), however, when there are several NPCs colliding with one another, my FPS drops a ton.

hitboxes:FindFirstChild("RightTendon").Touched:Connect(function(hit)
	if hit.Name == "LBlade" or hit.Name == "RBlade" and not hit.Parent:FindFirstChild("UpperTorso") then
		if hit.Transparency == 0 then
			if hit:FindFirstChild("Trail").Enabled == true then
				if hit.Trail.Enabled == true and not righttendoncut and hit.Parent.Parent.Parent:FindFirstChild("Boosting").Value == true then
					hitboxes:FindFirstChild("RightTendon"):FindFirstChild("Slashed"):Play()
					hit.Parent.Parent.Parent:FindFirstChild("Boosting").Value = false
					righttendoncut = true
					for i,v in pairs(hitboxes:FindFirstChild("RightTendon"):GetDescendants()) do
						if v:IsA("ParticleEmitter") then
							v.Enabled = true
						end
					end
					if lefttendoncut then
						tendonscut = true
						values:FindFirstChild("Tendonscut").Value = true
					end
					task.wait(8)
					if not tendonscut then
					righttendoncut = false
					for i,v in pairs(hitboxes:FindFirstChild("RightTendon"):GetDescendants()) do
						if v:IsA("ParticleEmitter") then
							v.Enabled = false
						end
					end
					end
				end	
			end
		end
	end
end)```

I really wanna know how to fix this, please and thank you

You are using a lot of :findFirstChild(“”) variables,
Maybe you could just use hit:GetAttribute(‘’)<
by giving your things attributes, which would work better than this.

1 Like

how exactly would i use :getattribute()?

Attributes are way better then checking through multiple Properties.

  • Your scripts are using lot of for i,v loops, which make them lag like hell.!
  • inside your script I would prefer an debounce for the touched object!
local debouncesystem = {}

hitboxes:FindFirstChild("RightTendon").Touched:Connect(function(hit)
	if hit.Name == "LBlade" or hit.Name == "RBlade" and not hit.Parent:FindFirstChild("UpperTorso") then
if debouncesystem[hit.Parent] then return end
		if hit.Transparency == 0 then
			if hit:FindFirstChild("Trail").Enabled == true then
				if hit.Trail.Enabled == true and not righttendoncut and hit.Parent.Parent.Parent:FindFirstChild("Boosting").Value == true then
debouncesystem[hit.Parent] = true
					hitboxes:FindFirstChild("RightTendon"):FindFirstChild("Slashed"):Play()
					hit.Parent.Parent.Parent:FindFirstChild("Boosting").Value = false
					righttendoncut = true
					for i,v in pairs(hitboxes:FindFirstChild("RightTendon"):GetDescendants()) do
						if v:IsA("ParticleEmitter") then
							v.Enabled = true
						end
					end
					if lefttendoncut then
						tendonscut = true
						values:FindFirstChild("Tendonscut").Value = true
					end
					task.wait(8)
					if not tendonscut then
					righttendoncut = false
					for i,v in pairs(hitboxes:FindFirstChild("RightTendon"):GetDescendants()) do
						if v:IsA("ParticleEmitter") then
							v.Enabled = false
						end
					end
debouncesystem[hit.Parent]=false
					end
				end	
			end
		end
	end
end)```

I really wanna know how to fix this, please and thank you

yeah your script can be simplified alot like this for example:

local hitboxes = script.Parent
local RightTendon = hitboxes:FindFirstChild("RightTendon")
local SlashedAnimation = RightTendon.FindFirstChild("Slashed")

function setRightTendonsParticles(bool: boolean)
	for i,v in RightTendon:GetDescendants() do
		if v:IsA("ParticleEmitter") then
			v.Enabled = bool
		end
	end
end

RightTendon.Touched:Connect(function(hit)
	local boosting = hit.Parent.Parent.Parent:FindFirstChild("Boosting")

	if hit.Name ~= "LBlade" or hit.Name ~= "RBlade" and hit.Parent:FindFirstChild("UpperTorso") then
		return
	end
	if hit.Transparency ~= 0 or hit:FindFirstChild("Trail").Enabled ~= true or not righttendoncut or boosting.Value ~= true then
		return
	end

	boosting.Value = false
	SlashedAnimation:Play()
	righttendoncut = true
	setRightTendonsParticles(true)

	tendonscut = lefttendoncut
	values:FindFirstChild("Tendonscut").Value = tendonscut
	task.wait(8)
	righttendoncut = tendonscut
	setRightTendonsParticles(tendonscut)
end)```

I don’t really see what could be causing much lag tbh, maybe try adding a debounce, the guard clauses should stop the function running if it touches an invalid object