Random ParticleEmitter enabling

I’m trying to create a system where parts are randomly placed around the map and ParticleEmitters are randomly enabled every few seconds, but I can’t seem to enable them, as it says I’m indexing Enabled with nil:

local particle = nil

local function getParticle()
	for i, v in pairs(workspace.RandomExplosions:GetDescendants()) do
		if v:IsA("ParticleEmitter") then
			v = particle
		end
	end
	return particle
end

while wait(math.random(1, 5)) do
	getParticle()
	wait(0.1)
	particle.Enabled = true
	wait(0.1)
	particle.Enabled = false
end
local particle = nil

local function getParticle()
	for i, v in pairs(workspace.RandomExplosions:GetDescendants()) do
		if v:IsA("ParticleEmitter") then
			v = particle
		end
	end
	return particle
end

while wait(math.random(1, 5)) do
	particle = getParticle()
	wait(0.1)
	particle.Enabled = true
	wait(0.1)
	particle.Enabled = false
end

You forgot to call the function to a variable

You wouldn’t need to define particle if you’d returned it in the calling function, which I have done

local function getParticle()
for i, v in pairs(workspace.RandomExplosions:GetDescendants()) do
if v:IsA(“ParticleEmitter”) then
v = particle --This changed to this
particle = v
end
end
return particle
end

That’s great, it works, but it only works for one part now

Try this code:

function setParticle(bool)
	for i, v in pairs(workspace.RandomExplosions:GetDescendants()) do
		if v:IsA("ParticleEmitter") then
			v.Enabled = bool
		end
	end
end

while wait(math.random(1, 5)) do
	wait(0.1)
	setParticle(true)
	wait(0.1)
	setParticle(false)
end

That code activates all of the emitters all at once.

Can you said me what you want that the particles do?

I need the particles to randomly enable between a certain time period

function setParticles()
	for i, v in pairs(workspace.RandomExplosions:GetDescendants()) do
		if v:IsA("ParticleEmitter") then
            wait(math.random(1, 5))
            v.Enabled = true
            wait(0.1)
            v.Enabled = false
		end
	end
end

while wait() do
	setParticles()
end