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 ?
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 ?
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:
If you want to stop a function you can simply use return.
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.
That’s not true. You put it after each wait like I wrote
You can return once but have multiple return conditions
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.
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
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.
i mean stop it while its running ------------------------
Yeah just set a variable
local Cancel = false
local function MyStyle()
if Cancel then return end
print("I have style.")
end
Cancel = true
MyStyle()
yeah that’s really annoying to do almost every single lines , isn’t there any other ways to do so ?
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.
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.