I haven’t finished it yet, but for example, if I use Emit(object, false) and at the same time Active(key: string, mainObject: Instance, enableList: {string}), there might be confusion regarding the duration.
This is because Emit activates emitThread, which if there is a duration enables and then disables. So, there could be confusion, and it might disable at the same time I call my Active() function.
Thanks for your responses.
Here are the code examples + my module
local function emitThread(particle: ParticleEmitter)
local emitCount = particle:GetAttribute("EmitCount") or 0
local emitDelay = particle:GetAttribute("EmitDelay") or 0
local emitDuration = particle:GetAttribute("EmitDuration") or 0
task.delay(emitDelay, function()
particle:Emit(emitCount)
if emitDuration > 0 then
particle.Enabled = true
task.wait(emitDuration)
particle.Enabled = false
end
end)
end
local VFX = {}
VFX._Cache = {}
function VFX:GetListOfVFX(object: Instance): {ParticleEmitter}
local listOfVFX = {}
for _, particle in object:GetDescendants() do
if not particle:IsA("ParticleEmitter") then continue end
table.insert(listOfVFX, particle)
end
return listOfVFX
end
function VFX:Emit(listOfVfx: {ParticleEmitter}, waitEnd: boolean)
local sortDurations = {}:: {number}
for _, particle in listOfVfx do
local duration = particle:GetAttribute("EmitDuration") or 0
local emitDelay = particle:GetAttribute("EmitDelay") or 0
local maxLifeTime = particle.Lifetime.Max
local addToSortDurations = (duration + maxLifeTime + emitDelay)
task.spawn(emitThread, particle)
table.insert(sortDurations, addToSortDurations)
end
table.sort(sortDurations, function(a, b)
return a > b
end)
if waitEnd then
task.wait(sortDurations[1])
end
print("end")
end
function VFX:Active(key: string, mainObject: Instance, enableList: {string})
local listOfVfx = {}
--VFX._Cache[key] = mainObject
if enableList then
local list = {}
for _, name in enableList do
local part = mainObject:FindFirstChild(name)
if not part then warn("part not exist") continue end
local descendants = part:GetDescendants()
table.move(descendants, 1, #descendants, #list + 1, list)
end
for _, particle in list do
if not particle:IsA("ParticleEmitter") then continue end
table.insert(listOfVfx, particle)
end
else
listOfVfx = VFX:GetListOfVFX(mainObject)
end
for _, particle in listOfVfx do
particle.Enabled = true
end
end
function VFX:Desactive()
end
return VFX
--exemple
function module.Start()
task.wait(5)
local list = VFX:GetListOfVFX(test)
VFX:Emit(list, false)
VFX:Active("test", test, {"ground", "uopper"})
end