[Solved]Any ways to stop a function?

Hello , i’ve been wondering if we could stop a function from working

for exemple :

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

is there any ways i can possibly stop the function ?

11 Likes

I’m not sure what you mean here. Please can you be more specific. Are you trying to stop a function from executing while it is running, or stop it from starting?

Please review the following to see how you can post properly:

1 Like

If you want to stop a function you can simply use return.

9 Likes

Yeah what i would like to do is basically stop a function while it is running.

Do you want to do this from inside the function, or from another function?

What I would do is putting if stop == true then return end after each wait

I’d like to know both ways to be honest. -----------

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.
https://developer.roblox.com/articles/Beginners-Guide-to-Coroutines
https://developer.roblox.com/api-reference/lua-docs/coroutine

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

4 Likes