How would I stop a function from running?

Hello! I was wondering how to stop a function from running whenever a value is changed. This is probably very simple but I just can’t seem to figure it out. Here is an example of what I want to accomplish:

--example (in a module script)

local module = {}
local bool = game.workspace.BoolValue

function module.PrintNumbers()
    
    task.spawn(function()
        bool.Changed:Connect(function()
            if bool.Value == true then
                --stop this function from continuing
            end
        end)
    end)

    print(1)
    task.wait(1)
    print(2)
    task.wait(1)
    print(3)
    task.wait(1)
    print(4)
    task.wait(1)
    print(5)
    task.wait(1)
    print(6)
    task.wait(1)
    print(7)
    task.wait(1)
    print(8)
    task.wait(1)
    print(9)
    task.wait(1)
    print(10)
end

return module

If anyone could help, that would be greatly appreciated!

use return. To give a example;

task.spawn(function()
    bool.Changed:Connect(function()
        if bool.Value == true then
           return
           --Use it where you want to as if statement maybe ? changes with what you want
        end
    end)
end)
2 Likes

Oops. Forgot to say I already figured it out. I’ll give you the solution because it’s the same thing I did.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.