Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Using wait() in ModuleScripts?

Asked by 8 years ago

So, I've come across the need to add a wait() function in a ModuleScript. I read on the wiki that when wait() is used in a ModuleScript, all other functions/scripts requiring the aforementioned ModuleScript will also wait. At least, that's my understanding of it. Is there a way to get around this?

What I'm attempting to do is have a for-loop pause what it's doing until a certain value becomes true.

For Example,


function GameManager:StartGame() local Rainbow = false --Skipping many lines of unrelated code function ActuallyBeginGame() for i = 1,Rounds do SpawnThing1() SpawnThing2() SpawnThing3() repeat wait() until Rainbow == true --If I did this right, the for loop should wait to execute any other code until Rainbow is true Rainbow = false --Make Rainbow false to start the process over again. end end end

I cant really test my game out at the moment, so I haven't tried experimenting on my own to see whether it works or not. I would use spawn(function), but since that just runs code instantly on a separate thread I'm afraid it wouldn't pause the for-loop.

So would using wait pause the whole module script, or just the for loop? Also, if it does, is there another way to do this? Any help is appreciated, and if anything needs clarifying let me know. Thanks! :)

P.S. If this is confusing, or something needs clarification, let me know. Thanks.

1 answer

Log in to vote
0
Answered by 8 years ago

I'll explain how this works with a few example modules.

The module structure the wiki describes:

local module = {}
wait(2)
return module

When this module is required it is forced to wait two seconds. Once the wait is done it will return instantly to any script that was waiting on it and any that will require() the module later.

The module structure that you are using:

local module = {}
module.waitTwoSeconds = function()
wait(2)
end
return module

This module will instantly return to any script that required it. If a script would then call waitTwoSeconds(), it will only cause that script to wait.

So as long as the module doesn't use a wait function before it returns it will not cause any other script to wait for it.

0
Oh ok, thanks! :) AwsomeSpongebob 350 — 8y
Ad

Answer this question