Printing keeps repeating almost a thousand times then stops

image

Why does this keep on repeating?

local aoe = workspace:GetPartsInPart(sphere)
	
	for i, a in pairs(aoe) do
		if a:IsA("BasePart") and a.Name ~= "HumanoidRootPart" then
			if a:IsDescendantOf(workspace.NPCs) then
				if a.Parent:FindFirstChildOfClass("Humanoid") then
					if not table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d:IsA("BasePart") or d:IsA("Decal") and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 1})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = false
							end
							table.insert(targets, a.Parent)
							print("STARTED NPC")
						end
					end
					wait(10)
					if table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d:IsA("BasePart") or d:IsA("Decal") and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 0})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = true
							end
							local newtween = tweenservice:Create(sphere, tweeninfo, {Size = Vector3.new(0, 0, 0), Transparency = 1})
							newtween:Play()
							newtween.Completed:Wait()
							sphere:Destroy()
							print("ENDED NPC")
						end
					end
				end
			elseif a:IsDescendantOf(workspace.Players) then
				if a.Parent:FindFirstChildOfClass("Humanoid") then
					if not table.find(targets, a.Parent) then
						local player = game.Players:GetPlayerFromCharacter(a.Parent)
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d:IsA("BasePart") or d:IsA("Decal") and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 1})
								tween:Play()
								remote:InvokeClient(player, 0.7)
							elseif d:IsA("BillboardGui") then
								d.Enabled = false
							end
							table.insert(targets, a.Parent)
							print("STARTED PLAYER")
						end
					end
					wait(10)
					if table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d:IsA("BasePart") or d:IsA("Decal") and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 0})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = true
							end
							local newtween = tweenservice:Create(sphere, tweeninfo, {Size = Vector3.new(0, 0, 0), Transparency = 1})
							newtween:Play()
							newtween.Completed:Wait()
							sphere:Destroy()
							print("ENDED PLAYER")
						end
					end
				end
			end
		end
	end

I want it so it only inserts into the table once and print it once, not 100+ times.
Any solution?

Your print is inside a loop, keep it out of the loop, or make a debounce.

I’ve tried keeping it out of the loop and even putting it inside the if statements but I just can’t seem to fixi t

Create a debounce var in the start of the script, don’t put it inside the loop or it’ll change to false every iteration: debounce = false

					if debounce == false then
						debounce = true
						print("STARTED NPC")
					end

Where would I put that if statement? I want it to print “STARTED NPC” if it’s an npc and not a player.

local debounce = false
	local aoe = workspace:GetPartsInPart(sphere)
	
	--[[ if debounce == false then
						debounce = true
						print("STARTED NPC")
					end ]]
	
	for i, a in pairs(aoe) do
		if a:IsA("BasePart") and a.Name ~= "HumanoidRootPart" then
			if a:IsDescendantOf(workspace.NPCs) then
				if a.Parent:FindFirstChildOfClass("Humanoid") then
					if not table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d:IsA("BasePart") or d:IsA("Decal") and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 1})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = false
							end
							table.insert(targets, a.Parent)
							print("STARTED NPC")
						end
					end
					wait(10)
					if table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d:IsA("BasePart") or d:IsA("Decal") and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 0})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = true
							end
							local newtween = tweenservice:Create(sphere, tweeninfo, {Size = Vector3.new(0, 0, 0), Transparency = 1})
							newtween:Play()
							newtween.Completed:Wait()
							sphere:Destroy()
							print("ENDED NPC")
						end
					end
				end
			elseif a:IsDescendantOf(workspace.Players) then
				if a.Parent:FindFirstChildOfClass("Humanoid") then
					if not table.find(targets, a.Parent) then
						local player = game.Players:GetPlayerFromCharacter(a.Parent)
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d:IsA("BasePart") or d:IsA("Decal") and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 1})
								tween:Play()
								remote:InvokeClient(player, 0.7)
							elseif d:IsA("BillboardGui") then
								d.Enabled = false
							end
							table.insert(targets, a.Parent)
							print("STARTED PLAYER")
						end
					end
					wait(10)
					if table.find(targets, a.Parent) then
						for i, d in pairs(a.Parent:GetDescendants()) do
							if (d:IsA("BasePart") or d:IsA("Decal") and d.Name ~= "HumanoidRootPart") then
								local tween = tweenservice:Create(d, tweeninfo, {Transparency = 0})
								tween:Play()
							elseif d:IsA("BillboardGui") then
								d.Enabled = true
							end
							local newtween = tweenservice:Create(sphere, tweeninfo, {Size = Vector3.new(0, 0, 0), Transparency = 1})
							newtween:Play()
							newtween.Completed:Wait()
							sphere:Destroy()
							print("ENDED PLAYER")
						end
					end
				end
			end
		end
	end

in your current print statements:
image

also the script you have is pretty messy and cluttered, you should try to compress the if statements

I also have another problem, when you do the move, it doesn’t set the characters inside’s parts transparency to 1 again since they were probably in the table. (it also doesnt print the started and ended thingy)