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

Is there anyway to stop a function externally?

Asked by 5 years ago

I have no idea how to stop a function. Is there any way to do so?

function fish()
    print("dog")
    wait(1)
    print("cat")
    wait(1)
    print("yes")
end

while wait() do
    if health.Value == 0 then
        fish:Stop()  -- Is there anyway to do something like this?
    end
end
0
you can just ignore the function or put debounce on the fish() greatneil80 2647 — 5y
0
ew don't use wait() as your loop's condition User#19524 175 — 5y
0
incapaz, why? SirDerpyHerp 262 — 5y
0
Using 'wait()' as a while condition apparently slows down performance by some (practically unnoticeable) amount because Lua has to do a couple extra steps to ensure that 'wait()' actually returns a non-false/nil value. I doubt you'd notice the difference even if you had thousands of such loops running simultaneously. chess123mate 5873 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

So long as you have special code in the function you wish to potentially stop, yes. You simply need to have a variable to track whether or not the function should return early and then check that variable after every command that yields (wait, GetAsync, etc). ex:

local stopFish
local function fish()
    stopFish = false
    print("dog")
    wait(1)
    if stopFish then return end
    print("cat")
    wait(1)
    if stopFish then return end
    print("yes")
end

while wait() do
    if health.Value == 0 then
    stopFish = true
    end
end

0
Good answer. The term you're looking for is 'debounce'. User#19524 175 — 5y
0
@incapaz: Thanks, but 'debounce' is for preventing the same function from running multiple times when you only want it to run once, not for stopping a function part-way through. chess123mate 5873 — 5y
Ad

Answer this question