Script runs but doesn't do what I scripted

I have a script that gets all descendance of a model and checks if it’s something, do this. Specifically, if it’s a particle emitter, add it to a table, and for each particle emitter in the table, Emit 4 particles 5 times. Thing is, it only emits out of 1 of the particles, and just pretends the other one doesn’t exist.

Script:

module.AnimatePressurePlateOn1 = function(ins:Model) -- Pretend the "ins" is "Pillar1" as seen in the video from the Explorer
	local tweenOn = {}
	local particleEmitters = {}
	local num = 0
	
	for _,obj in ins:GetDescendants() do
		if obj:IsA("Part") or obj:IsA("UnionOperation") then
			local tween = tweenService:Create(obj,module.infoSaves.info4,
				{
					Transparency = 0,
					CanCollide = true
				})
			table.insert(tweenOn,tween)
			continue
		end
		if obj:IsA("ParticleEmitter") then
			table.insert(particleEmitters,obj)
		end
	end
	
	for _,tween in tweenOn do
		tween:Play()
	end
	
	for _,particle in particleEmitters do
		while num < 5 do
			particle:Emit(4)
			num+=1 
		end
	end
end

Video:


Things I’ve done

  1. Check it it’s actually part of the table, i.e.
	for _,particle in particleEmitters do
		print(particle)
		while num < 5 do
			particle:Emit(4)
			num+=1 
		end
	end

yes, I have, it prints 2 things, so I know damn well it’s in there.

  1. Use task.spawn() i.e.
	for _,particle in particleEmitters do
		task.spawn(function()
			while num < 5 do
				particle:Emit(4)
				num+=1 
			end
		end)
	end

yes, I have, still same problem still happens.

  1. Changing the name one of the emitters, yes, same problem happens

In short, the script is detecting both emitters, but refuses to acknowledge one of them for no reason, at least for what I can think of. Stupidest part yet, I made a similar set up in a different script, AND IT WORKS, WTF AM DOING WRONG HERE THEN>???

1 Like

it’s because you are using a while loop to :Emit each emitter 5 times
what happens is num gets increased to 5 after the first particleEmitter, then all the next ones are skipped because num is already 5
i would use a for loop instead of a while loop for your purpose

1 Like

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