How to return a value from a modulescript that is in a loop, without breaking said loop

As I currently am making something that is dynamic and part of a package
I want to know if there is actually a way to do this
Code:
Screenshot:
image
CodeBlock:

function consider(Key: string,interval = 300, iterations: number) {
    if (iterations !== undefined) {
        print("Condider")
        if (iterations>0){
            for (let i = 0; i < iterations; i++) {
                if (interval !== 0) {
                    wait(interval)
                }
            }
        }
        
    } else {
        print("Considered")
        while (true) {
            if (interval !== 0) {
                wait(interval)
            }
            let featureFlag = dynamicFeatureFlagDataStore.GetAsync(Key)
            if (featureFlag === undefined) {
                return warn("Couldn't find the named DFFlag")
            } else {
                consider(Key, interval, iterations);
                return featureFlag
            }
        }
    }
}
1 Like

I think you would need to do some workaround such as setting some external IntValue, which would probably defeat the purpose of using a module script. Once a module script returns something, it’s done as far as I know.

You can use the Roblox Global Spawn(callback). This will cause the Roblox Lua scheduler to call into the provided function after your module as returned. If the behavior is the same, I believe it will call your function on the next game loop in which Lua is run.

1 Like

That being said, it will still break your loop. You can convert your loop to recursion and include the current iteration variables as upvalues in the callback provided to Spawn. Or you could use module-level upvalues instead of passing the iteration variables through the arguments. Instead of doing recursion you could just save the current loop state before you return and then restore it in the callback.

Options, options, options!

When starting it on a different thread literally nothing is returned

1 Like

Right, you will have to return on the main thread and continue the computation on another.