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
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.)
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