Execution Time in Module Functions, need clarification

Hello guys,

Once again I find myself in need for advise from more experienced devs.

Long story short, I got a few ParticleEmitters that need to instantiated or dumped into the workspace in different times, it’s a cosmetic need (I don’t want these Emitters to be firing at the very same time).

So I wrote a tiny module, code below:

local RandomizedSpawn = {}

function RandomizedSpawn.Spawn (entity, minTime, maxTime, coordinates, wrapper)

local clonedEntity = entity:Clone()
local waitInterval = math.random(minTime, maxTime)

wait(waitInterval)

clonedEntity.Position = coordinates
clonedEntity.Parent = wrapper

end

return RandomizedSpawn

And then from my script, I call like that:

randomizedSpawn.Spawn(DarkLeaf01, .5, 2, Vector3.new(-30.27, 17.604, -45.42), game.Workspace.Runtime.Emitters)
randomizedSpawn.Spawn(DarkLeaf02, .5, 2, Vector3.new(-20.52, 17.604, -53.05), game.Workspace.Runtime.Emitters)
randomizedSpawn.Spawn(DarkLeaf03, .5, 2, Vector3.new(-21.43, 17.604, -68.42), game.Workspace.Runtime.Emitters)
randomizedSpawn.Spawn(DarkLeaf04, .5, 2, Vector3.new(-38.1, 17.604, -93.36), game.Workspace.Runtime.Emitters)
randomizedSpawn.Spawn(LightLeaf01, .5, 2, Vector3.new(-26.71, 17.604, -37.18), game.Workspace.Runtime.Emitters)
randomizedSpawn.Spawn(LightLeaf02, .5, 2, Vector3.new(-16.9, 17.604, -51.13), game.Workspace.Runtime.Emitters)
randomizedSpawn.Spawn(LightLeaf03, .5, 2, Vector3.new(-33.97, 17.604, -57.93), game.Workspace.Runtime.Emitters)
randomizedSpawn.Spawn(LightLeaf04, .5, 2, Vector3.new(-33.97, 17.604, -74.32), game.Workspace.Runtime.Emitters)

My expectation was that the Spawn functions would be executed at once, and then wait for whatever is the delay and clone the Emitter to the workspace, but what is happening is that one call is waiting for the previous call to run… therefore the code block above can take up to 16 seconds to run… where what I wanted / was expecting was for the code block above to take maximum of 2 seconds to run.

Hopefully it make sense.

So the question is, how can I get the desired effect?

Thanks in advance.

EDIT: I’m currently storing these Emitters in the replicated storage.

Another thing I found myself thinking: Since these Emitters are not really relevant, they should live in the client (I’d assume).

And if that’s the case, is there any advantage in have them spawned by a script other than be able to start them in different times?

Oh!
Since the module yields, after the first function you called, it will wait and then spawn then it continues…

To fix that, use the spawn() function!

There are many alternatives to spawn like coroutine.wrap(insert_function)(parameters)

Example code using spawn:

spawn(function()
    randomizedSpawn.Spawn(DarkLeak02, .5,.2, Vector3.new(parameters_here), game.Workspace.Runtime.Emitters)
end)

it will not yield.

Example Code using coroutine.wrap():

coroutine.wrap(randomizedSpawn.Spawn)(parameters here)

This will also not yield.

2 Likes

Awesome, works beautifully.

One more question, what is the main difference between spawn and coroutine.wrap()

Thank you.

Here’s some info about why you shouldn’t use spawn:

Please do not use spawn . It’s a hold-over from Roblox’s 30hz pipeline and has no guarantees about when (or if) it will begin executing the new thread. spawn is especially evil because it seems innocent at first, but once you have a lot of code that all uses spawn , you can see significant delays in execution upwards of several seconds in a resource-intensive game. Roblox throttles execution of threads and if the device is too bogged down, your thread could just get deferred forever.

This is bad advice. It’s easy to answer when you should use spawn: never. There is never a situation where you want to run some code “maybe at some point in the future or never”. Even for things that seem inconsequential, you can end up shooting yourself in the foot if things run in an order that you don’t expect them to.

Don’t set yourself up for failure. Use coroutine.wrap if you need to begin a new thread. Never use spawn , delay , or wait() in your code.

There’s a full post here: Coroutines V.S. Spawn()... Which one should I use? - #8 by evaera

1 Like