Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there anyway to stop a function externally?

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

4 comments

0

you can just ignore the function or put debounce on the fish()

0

ew don't use wait() as your loop's condition

0

incapaz, why?

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.

1 answer

✓ Accepted answer

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

2 comments

0

Good answer. The term you're looking for is 'debounce'.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.