'If' Statement Stuck After Running Generic Loop

Hi, this is a trigger located in a LocalScript in StarterGui that is listening for when a player clicks a button on their screen. When they do, it loops through Workspace to find any objects that match with the given class name and then executes code there onwards. But everytime I run it, the code will not get passed the if statement. I’ve tried replacing the directive objects with sounds and scripts and the code has run perfectly fine but when I explicitly mention ParticleEmitters or Smoke (Which are already loaded into the game at the point that the function is fired), it just does not work. Can someone please help?

ParticlesImage.MouseButton1Click:Connect(function()
	Click:Play()

	for _, Particle in pairs(game.Workspace:GetDescendants()) do
		if Particle:IsA("ParticleEmitter") or Particle:IsA("Smoke") then
			-- Code
		end
	end
end)
2 Likes

try:

ParticlesImage.MouseButton1Click:Connect(function()
	for _, Particle in pairs(workspace:GetDescendants()) do
		if not Particle:IsA("ParticleEmitter") and not Particle:IsA("Smoke") then continue end
	    -- Code
	end
   Click:Play()
end)
1 Like

Hi there. Thank you so much for your reply, it worked! I know you’ve already given me the solution I need but could you maybe explain to me why the one I did didn’t work but yours did when they do the same thing but with one extra step? I just want to know so I can learn as well :slight_smile:

1 Like

I don’t know why but I have two reasons why it might have worked over yours:

  • Optimised - My script is cleaner and shorter. I used the keyword ‘workspace’. Using ‘continue’ skips iteration for this ‘Particle’.
  • Click:Play() - I moved Click:Play() so that it runs after the for loop. It may have interfered with the loop so that the loop couldn’t run. You could put the loop in a coroutine so it works but it’s probably easier to move Click:Play().
1 Like

Ah, I see now. Thank you so much for the reply, this helped a lot!

1 Like

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