Small Loop creates more than 5/7 particles

So, i have an loop that creates around 5 or 7 parts, but the problem is that it creates more than 5 or 7, why this occurs?

script:

for partAdd = 1, math.random(5, 7) do
					local Effect = meshes1.DropNeonw:Clone()
					Effect.Parent = workspace.Terrain
					Effect.CanCollide = false
					Effect.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.Angles(math.rad(math.random(-180, 180)), math.rad(math.random(-180, 180)), math.rad(math.random(-180, 180)))

					--Effect.Splatter:Play()

					local EffectVelocity = Instance.new("BodyVelocity", Effect)
					EffectVelocity.MaxForce = Vector3.new(1, 1, 1) * 1000000;
					EffectVelocity.Velocity = Vector3.new(1, 1, 1) * Effect.CFrame.LookVector * math.random(20, 40)

					debris:AddItem(EffectVelocity, 0.2)
					debris:AddItem(Effect, 3)
				end

Your loop itself works correctly, I just tested it. The loop itself always triggers 5-7 times, however, the code outside of this loop is failing and probably creating more loops, but without seeing that code I cannot help you.

i guess, but if i do math.random(1,2), it should create 1 or 2 parts, but no, it creates more than 1 or 2 parts!

I can’t help because the loop itself works, I copied your code (and just now tested the 1, 2 case) and it still works just fine, I can see the loop triggering the amount of times specified.

this is the full code

local dashcanpassthrough = {} -- the parts the dash can pass through
wait(0.1)
--local rs = game:GetService("ReplicatedStorage")

local thunderclapevent = game.ReplicatedStorage:WaitForChild("Gear2re")
local tweenService = game:GetService("TweenService")

local tweenservice = game:GetService("TweenService")

local runservice = game:GetService("RunService")

local debris = game:GetService("Debris")
local meshes1 = game.ReplicatedStorage:WaitForChild("Gear2")

local meshes = meshes1:WaitForChild("vfx")

local animation = script:WaitForChild("Animation")



local damage = 20 -- set your damage here

local maxdistance = 100

thunderclapevent.OnServerEvent:Connect(function(player,camera, cooldown)

	local character = player.Character
	local humanoid =  character.Humanoid
	local humrp =   character.HumanoidRootPart
	
	if not character.IsGear2.Value then
		character.IsGear2.Value = true
		
	local thunderanim = humanoid:LoadAnimation(animation.thunderclapanim)
--	humrp.Anchored = true
	thunderanim:Play()
--  character.hum:Play()
	character.HumanoidRootPart.Anchored = true

	for i = 1,7 do
		wait(0.03)
	local thunderbreath = workspace.Dummyzenitsu.Head.attachmentnreathing:Clone()
	thunderbreath.Sparks1:Emit(15)
	thunderbreath.ParticleEmitter:Emit(5)
		thunderbreath.Parent = character.Head
		debris:AddItem(thunderbreath,1.5)
	end
	--local bru = workspace.Dummyzenitsu.Head.A1:Clone()
	--local bru2 = workspace.Dummyzenitsu.Head.A2:Clone()
	--bru.Parent = character.Head
--	bru2.Parent = character.Head
	
	
	
	--debris:AddItem(bru,1.5)
	--debris:AddItem(bru2,1.5)


	--thunderanim:GetMarkerReachedSignal("thunderclap"):Connect(function()
	
	local thunderclapfolder = Instance.new("Folder")
	thunderclapfolder.Name = player.Name.." used Gear 2"
	thunderclapfolder.Parent = workspace
	debris:AddItem(thunderclapfolder,4)
		
		character.gear2so:Play()
	wait(1.2)
	local clone = meshes:Clone()
	clone.Parent = workspace
	clone:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)-- character.HumanoidRootPart.CFrame
	debris:AddItem(clone, 1.7)
	
	for i, v in pairs(clone:GetChildren()) do
	--	if v:IsA("BasePart") or v:IsA("Part") then
		local tInfo = TweenInfo.new(0.4, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)
		local tInfo2 = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0)

			local t = tweenService:Create(v, tInfo, {Transparency = 1})
		local t2 = tweenService:Create(v, tInfo, {Size = Vector3.new(0,5,0)})
		for index, e in pairs(player.Character:GetChildren()) do
			if e:IsA("BasePart") then
				
				local t3 = tweenService:Create(e, tInfo2, {Color = Color3.new(1, 0.580392, 0.580392)})
				t3:Play()
				local clone1 = e:Clone()
				clone1.Parent = workspace.Terrain
				clone1.CanCollide = false
				clone1.Anchored = true
				clone1.Material = Enum.Material.Neon
				clone1.Color = Color3.new(1, 1, 1)
				local t31 = tweenService:Create(clone1, tInfo2, {Size = Vector3.new(0,5,0)})
				local t32 = tweenService:Create(clone1, tInfo2, {Transparency = 1})
				local t33 = tweenService:Create(clone1, tInfo2, {CFrame = CFrame.new(math.random(20,50),math.random(30,70),math.random(40,80))})

				t31:Play()
				t32:Play()
				t33:Play()

				debris:AddItem(clone1, 0.75)
				for partAdd = 1, math.random(1, 2) do
					local Effect = meshes1.DropNeonw:Clone()
					Effect.Parent = workspace.Terrain
					Effect.CanCollide = false
					Effect.CFrame = character:WaitForChild("HumanoidRootPart").CFrame * CFrame.Angles(math.rad(math.random(-180, 180)), math.rad(math.random(-180, 180)), math.rad(math.random(-180, 180)))

					--Effect.Splatter:Play()

					local EffectVelocity = Instance.new("BodyVelocity", Effect)
					EffectVelocity.MaxForce = Vector3.new(1, 1, 1) * 1000000;
					EffectVelocity.Velocity = Vector3.new(1, 1, 1) * Effect.CFrame.LookVector * math.random(20, 40)

					debris:AddItem(EffectVelocity, 0.3)
					debris:AddItem(Effect, 3)
					break
				end
				for o, c in pairs(clone1:GetChildren()) do
					if c:IsA("Motor6D") then
						c:Destroy()
					end
				end
				if workspace.Terrain:FindFirstChild("Head") then
					workspace.Terrain.Head:Destroy()
				end
			end
        end
			t:Play()
			t2:Play()
		
			task.wait(0.15)
			character.HumanoidRootPart.Anchored = false
			
		--end
	end	
		
		---------------------------------------------------------------------
	
	

	
	
		
		

		
		
		
		-- tweens 
		
		--end)

	
	
	
		thunderclapevent:FireClient(player)
	end
end)



here is your problem, you put the DropNeonw loop inside of the loop that’s being used for looping through the player’s character, this means that your loop is triggering 1-2 times every single time it loops through a part of your character, your loop is triggering 15+ times, to fix this, take the DropNeonw loop and place it under the end at line 125, right above t:Play()

1 Like