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

How to stop a function while its running?

Asked by 2 years ago

How to stop a function while its running?? I'm looking how to stop it from outside the function. Using return or break will stop from within the function, which is not what I'm really looking for.

Any help would be appreciated. Thanks in Advance!!

0
try taking a look at https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine it should work for this Benbebop 1049 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago

setfenv(func,{})

0
Works, but makes an error. I would like a more efficient approach to this. Shounak123 461 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

What you can probably do if its a while true loop or some other loop is check if a value you defined earlier is false or true

For example:

local Check = false

coroutine.wrap(function()
    while Check == false do
        print("Running")
        wait(0.1)
    end
end)()

wait(5)

Check = true -- This will make the function stop running

You could also use the same logic for a function you have defined:

local Check = false

function GoodFunction()
    coroutine.wrap(function()
        if Check == false then
            repeat
                print("Good")
                wait(0.1)
            until Check == true
        end
    end)()
end

GoodFunction()

wait(5)

Check = true

Let me know if you have any issues!

Answer this question