How would I make it so that a random ParticleEmitter in a group will be enabled then disabled?

I am working on creating some fire pyrotechnics and I’m trying to make it so that when a button is pressed or something is triggered it will get a random ParticleEmitter in the folder and enable it then de-enable it. I’ve tried using math.random but haven’t got that to work. What am I able to do?

Code:

local function StageFireEnabled(loudness)
		local LoudnessMeter = math.floor(loudness / 7000)
		for _,v in pairs(StageFire:GetChildren()) do
			v.Fire1.Fire.Enabled = true
		end
		wait(0.2 - LoudnessMeter)
		for _,v in pairs(StageFire:GetChildren()) do
			v.Fire1.Fire.Enabled = false
		end
	end 

Must’ve not use math.random properly, first property is the min, and second is the max.

    Fires = StageFire:GetChildren()
    ChosenFire = Fires[math.random(1, #Fires)]
    ChosenFire.Fire1.Fire.Enabled = true
    wait(0.2 - LoudnessMeter)
    ChosenFire.Fire1.Fire.Enabled = false

Just as an additional pointer, using ipairs is better then pairs when possible.

Anything that is indexed via continuous integers starting at 1 you can use ipairs. (It really only matters in cases where you need your code to be efficient.)

1 Like

I got an error saying attempted to get length of number value I think you made it so that it finds a random child in the folder but can’t enable it?

Code:

			for i,fireModel in pairs(StageFire:GetChildren()) do 
				local ChosenFire = i[math.random(1, #i)]
				ChosenFire.Fire1.Fire.Enabled = true		
			end

This is because you’re trying to use a key to get the random fire rather than the table.

This:

for i,fireModel in pairs(StageFire:GetChildren()) do 
	local ChosenFire = i[math.random(1, #i)]
	ChosenFire.Fire1.Fire.Enabled = true		
end

would become this:

local StageFires = StageFire:GetChildren()
local ChosenFire = StageFires[math.random(#StageFires)]
ChosenFire.Fire1.Fire.Enabled = true
task.wait(0.2 - LoudnessMeter) -- task.wait is the same as wait, just more accurate
ChosenFire.Fire1.Fire.Enabled = false	

This is basically the same as @polill00’s, so if this solves the problem then mark their post as the solution

1 Like

It seems something happened but only one fire particle activates instead of all of them randomly turning on and off

I’m a bit confused. Could you elaborate on what you want to happen? Where does the randomness come into play here?