Prevent Yielding With Module Or Event (Particles And Sounds)

Hello!

I’m trying to achieve the ability to play particles and play sounds using either a module or event that doesn’t require the initial script needing the particle to be enabled or sound to be played to yield.

The initial method I tried using was requiring a module script and adding the following functions:
function module.AddParticles(Part, Particle, Time, Color)
function module.PlaySound(TargetPart, TargetSound, Time)
Essentially this will clone the particles to the part, enable them, then wait the Time variable; After that, it’ll delete the particles.

The only flaw with the method above is that the script yields until the module is done deleting the particles or sound.

I’ve tried using bindable events since they do not yield, but the only issue with those is that the parameters don’t meet my criteria. They don’t support instances and that’s the main issue. I’ve tried requiring :FindFirstChild(Part, true) but I have multiple parts in workspace with the same name due to the part being a temporary instance.

If someone could educate me on a greater way to execute these functions using something, that would be great!

The reason why I want it formatted module wise or event wise is because due to the many lines in my scripting, it’ll be so much more neat to use it in a separate script and just :Fire() or require() it rather than adding the same lines to all of my scripts.

TLDR:
A way to play particle effects without yielding a script.

1 Like

Create a 2nd thread using “task.spawn()” or coroutines and load the “ParticleEmitter” instances or “Sound” instances through them, then use them in the main thread when necessary, this will prevent the loading of both types of instances from causing the main thread to yield when executing the script.

2 Likes

Great! You made me understand task.spawn() way better as I’ve read up on it but never thought about the idea of using it. Thank you.