[Solved]Any ways to stop a function?

If you want to do this, you would literally have to surround every line in this. Also, if the current line is yielding, then it will wait for it to complete before ending.

You can only return once. Return basically returns to the previous scope. Or “Returns control to the previous scope”. Which would be whatever called the function. If you wanted to stop a function externally you can simply set a debounce variable to true, and check for that variable inside of the function itself. If it’s true you can return.

Otherwise the general way to exit a function midway is to simply return.

To exit a function from within the function, use the return keyword. This, essentially, makes the function exit the current scope and stop.

From outside the function, you could try the method above suggested by @SirMing, although it has serious drawbacks.

2 Likes

That’s not true. You put it after each wait like I wrote

1 Like

You can return once but have multiple return conditions

1 Like

But that doesn’t work with any more complicated functions. You will need to check for the variable change after, pretty much, every line. We should try and think of a better solution.

1 Like

for exemple i would like another function that stops this script

function checker()	
	local plrvalue = #game.Players:GetPlayers()
   if plrvalue > 1 then
	script.Loading:Stop()
	Round()
	script.Loading:Stop()
else
	script.Text.Text = "Not enough players to start"
	script.Loading:Stop()
	script.Error:Play()
	wait(2)
	script.Loading:Play()
	script.Text.Text = "Loading..."
	wait(5)
	checker()
end
end
4 Likes

That is true, however in his example script.

function ex()
wait(1)
print("Test")
return
wait(2)
print("Test2")
return
end

Doesn’t make any sense, because only one would ever execute.

1 Like

i mean stop it while its running ------------------------

1 Like

Yeah just set a variable

local Cancel = false

local function MyStyle()
    if Cancel then return end
    print("I have style.")
end

Cancel = true
MyStyle()
3 Likes

yeah that’s really annoying to do almost every single lines , isn’t there any other ways to do so ?

1 Like

You don’t have to do it every line. You can have a function execute. But depending on what the function does, you don’t have to actually follow through with the code. Just program it in a way that you need to check for this variable at the last moment.

If you’re doing some kind of game lobby for instance. I would check if there’s enough players, and if there isn’t I would return. Now it’s possible someone could leave a queue before the game starts. So I would simply check if there’s enough players before the game starts. And if it isn’t simply go back to checking if there’s enough players.

Edit:
Since this is repeated code, actually checking if there’s enough players could be a function. If you find yourself reusing a lot of code. That’s an indicator that you should turn it into a function.

1 Like

Using an if statement to check for a boolean if it’s true or false after the waiting time should fix your problem temporarily. The caveat should be that it won’t stop the thread at all.

Your solution is in coroutines, which doesn’t stop completely.

In summary, there is no direct method to kill a function completely.

4 Likes

Okay thank you , now that i know that i can litterally cry and restart my script , have a good day lol.

1 Like

By using tick() and once a specific time past and a debounce is still true/false since it ain’t stopped yet, then print whatever, else if it’s not then, fire return to stop the function.

This works effectively because there’ll be less delays than while loops wait().

Use return. Read this Lua documentation.

1 Like

I’ve come across this problem in depth, actually. The current function you’re using can’t be stopped, you will have to rewrite your code in such a way that it can be. It’s hard to determine what you’re doing and I couldn’t really find it under the mound of repeat posts about return.

You should try switching to an event-based system or rewrite your system a little bit, such as freezing the game state to “Not enough players” until there are enough. You can then use a numeric for loop for a countdown, which can allow you to break the function after each step.

for i = 15, 0, -1 do
    if notEnoughPlayers then
        break
    else
        someTime = i
        wait(1)
    end
end

Or something.

1 Like

Oh hey! I forgot that you could use a for loop with yields to check!

Also note that break will stop the loop instead of the function. Use return, as it will end the function instead, providing the paragraph about what break and return is from the link @slothfulGuy sent earlier.

Example

local Boolean = false -- do something for the boolean to become true

local function StartTimer()
    local i = 0
    repeat -- 20 seconds, you could use the numeric loop addressed above instead of repeat
        if Boolean then
            return
        end
        wait(1/20)
    until i == 20 * 20
    print("Timed out")
end

return basically stops a function, but what if you want to pause it? use coroutine

local ex = coroutine.wrap(function()
wait(1)
print("Test")
coroutine.yield()
wait(2)
print("Test2")
end)
ex()

I haven’t read all of the replies because there are very many, but to stop a thread (a function is part of a thread, if you want to stop a function and not its entire thread then the only way is to return out) you have two options:

  1. either the thread is running and you stop it from itself: coroutine.yield() is the best way
  2. or you have to stop it externally when it is yielded (if other code is running, your thread is either finished or yielded, you can check which via coroutine.status(thread))

the second option is definitely more complex, @Operatik linked my thread specifically about coroutine.kill
I am posting here to add on that on top of one of the hacky functions mentioned in that thread, you can also just destroy the script the thread originated from

23 Likes