'For' loop not working properly (and maybe a bit overcomplicated)

I am currently making some kind of ELS system where you can duplicate parts with particle emitters in them so it is fully customizable.

I divided them in two separate groups: group1 and group2.
There are also two groups (again folders) with parts in them.
The two groups have to alternate between the material neon and glass, and the particle emitters between transparency 1 and .3

I have finally got the code to work, but now the ‘for’ loops just pick one item of each folder and I don’t understand why…
On top of that, the funtion looks a bit too overcomplicated to me, but I’m not sure.

My questions are:

  • Why is are the for loops only ‘selecting’ one particle emitter and one part of each of the 4 folders?

  • Is it possible to make the loops shorter?

My code is:

for i,a in pairs(EmitterGroup1:GetDescendants()) do
	for i,b in pairs(EmitterGroup2:GetDescendants()) do
		for i,c in pairs(PartsGroup1:GetChildren()) do
			for i,d in pairs(PartsGroup2:GetChildren()) do
				
				if a:IsA("ParticleEmitter") then
					if b:IsA("ParticleEmitter") then

						--// Here is my function changing properties of a,b,c,d
						--// It only picks one item of all 4 groups

					end
				end
			end
		end
	end
end

I think it is just a stupid fault of mine but I thought I should ask. Thanks for helping!

2 Likes

Could you show us what it looks like in the explorer?

With Instance:GetChildren() you need to do ipairs() instead of pairs(). I’m still confused on how you have your stuff sorted in the folders, so like @Frqsh said a screenshot of your explorer would be very helpful.

Maybe try something like:

local ws = game:GetService("Workspace")
local EmitterGroup1 = ws:WaitForChild("EmitterGroup1")
local EmitterGroup2 = ws:WaitForChild("EmitterGroup2")
local PartsGroup1 = ws:WaitForChild("PartsGroup1")
local PartsGroup2 = ws:WaitForChild("PartsGroup2")
local searchTable = {}

-- emitters

for _, eg1 in pairs(EmitterGroup1:GetDescendants()) do
	if eg1 then
		if not table.find(searchTable, eg1) then
			table.insert(searchTable, eg1)
		end
	end
end
for _, eg2 in pairs(EmitterGroup2:GetDescendants()) do
	if eg2 then
		if not table.find(searchTable, eg2) then
			table.insert(searchTable, eg2)
		end
	end
end

-- parts

for _, pg1 in pairs(PartsGroup1:GetChildren()) do
	if pg1 then
		if not table.find(searchTable, pg1) then
			table.insert(searchTable, pg1)
		end
	end
end
for _, pg2 in pairs(PartsGroup2:GetChildren()) do
	if pg2 then
		if not table.find(searchTable, pg2) then
			table.insert(searchTable, pg2)
		end
	end
end

-- combined

for _,	item in pairs(searchTable) do
		print(item)
end

Screen Shot 2023-11-15 at 9.36.10 PM

Screen Shot 2023-11-15 at 9.37.49 PM

It looks like your code is using nested loops to iterate through the descendants of the four groups (EmitterGroup1, EmitterGroup2, PartsGroup1, and PartsGroup2). The issue with your current implementation is that the innermost loop (the loop over PartsGroup2:GetChildren()) will fully complete before the next iteration of the loop over PartsGroup1:GetChildren() begins. This means that for each combination of a and b (ParticleEmitters from EmitterGroup1 and EmitterGroup2), you are only considering one pair of parts (c and d).

local emitters1 = EmitterGroup1:GetChildren()
local emitters2 = EmitterGroup2:GetChildren()
local parts1 = PartsGroup1:GetChildren()
local parts2 = PartsGroup2:GetChildren()

-- Ensure that the number of elements in each group is the same
assert(#emitters1 == #emitters2 and #parts1 == #parts2, "Groups must have the same number of elements")

for i = 1, #emitters1 do
    local a = emitters1[i]
    local b = emitters2[i]
    local c = parts1[i]
    local d = parts2[i]

    if a:IsA("ParticleEmitter") and b:IsA("ParticleEmitter") then
       -- Continue Handling the stuff here i guess or do whatever
    end
end


I’m sorry I forgot to do that!

I tried the ipairs() now, but it didn’t help anything… (I just replaced the pairs() with ipairs())
Again, I’m sorry that I forgot the explorer!

I think that might help, but because the stuff I do where the function begins is pretty much lines long it will make the script a lot too large and less controllable I think…

That would’ve worked if I didn’t want to make it customizable haha!
But because I want it to be as customizable as possible, I can’t guarantee same amounts of everything…

2 Likes

I finally know the problem: I have a while true do loop inside of it all. But how can I fix it? It needs to constantly run.

The code I got now:

	for i,b in pairs(Group2:GetDescendants()) do
		for i,c in pairs(PartsFolder1:GetChildren()) do
			for i,d in pairs(PartsFolder2:GetChildren()) do
				
				
				if a:IsA("ParticleEmitter") and b:IsA("ParticleEmitter") then
				
					while true do
						--// Here is my function which only picks one part of all 4 groups
					end
					
				end

			end
		end
	end
end```

I finally found it!! I inserted a task.spawn thingy and it now works! It uses all parts of all folders correctly.

	for i,b in pairs(Group2:GetDescendants()) do
		for i,c in pairs(PartsFolder1:GetChildren()) do
			for i,d in pairs(PartsFolder2:GetChildren()) do
				
				
				if a:IsA("ParticleEmitter") and b:IsA("ParticleEmitter") then
						
						task.spawn(function()
							while task.wait() do
								--// My function stuff
							end
						end)
				end
				
			end
		end
	end
end```
2 Likes

No such thing as overcomplicated you should nest more loops theres nothing more beautifdul

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.