I want to stop a function, using another function, for example see the following code
local function dosomething(condition)
check(condition)
--statements
end
i use return end normally after i check a condition to stop a function, but i want check() itself to return end and stop the dosomething() function,so i want check itself to return end somewhat like this
local function check(condition)
if condition then return return end end
end
i know that the above code is not correct but i want to do something like that, i did use if statement and made check() return true on condition and to trigger return end to stop the dosomething() function, like this
local function check(condition)
if condition then return true end
end
local function dosomething(condition)
if check(condition) then return end
--statements
end
but i would prefer to just call check() instead of checking wether it returned true or not and then returning end, so i am curious wether thats possible to do so.
you might be able to do something like this with coroutines
local check
local co = coroutine.create(function(...)
-- dosomething
end)
function check(condition)
if condition then coroutine.close(co) return false end
return true
end
coroutine.resume(co, ...)
When a function returns without a value specified it is returning nil, which would be the same as not having any return value at all.
Check () would be running in the same scope, but you could pass a reference to the thread (or get the thread some other way, possibly using getfenv) to then close/yield the current thread.
the stop function will correctly return coroutine.yield and stop the co thread, but this method comes up with the problems of thread dying and having to create coroutines everytime i run dosomething function, its not exactly a problem but i would have to create coroutines for all my functions and it would be better for me to just use if statements
If you’re trying to call another function then cancelling the one that’s currently running, then you can call that function, then return the current one, stopping it.
function a()
print("a")
end
function b()
a()
return
print("b")
end
b() --prints a but not b
For events, you do the exact same thing, but you call :Disconnect() on the current function instead of using return.
Thanks for the post, in the stop function i would like to first check a condition and if its false i want the statements made after calling the stop function to execute, which i cant do so from the code you posted
You call function B from function A, and you want to check if a statement in function B is false. If it is, then you want the other statements in function A to run?
So you want function A to stop when function B’s statement returns false. Okay, I see.
Try something like this:
function b()
print("Function B fired\n")
if false then --some statement
print("Function B returns \"false\"\n")
return false
else
print("Function B returns \"true\"\n")
return true
end
end
function a()
print("Function A fired\n")
local check = b()
if check == false then
print("Function A got \"false\", disabling\n")
return
else
print("Function A got \"true\", continuing\n")
end
print("Function A continues...\n")
end
a()
Function A fires function B.
Then, function B checks if a statement is false, (in this example, it’s always false)
If the statement says it’s false, then function B returns false to function A, otherwise it returns true.
Lastly, function A gets that information, and disables itself if function B returned false.
Thanks for the post, unfortunately it was not the answer i was looking for, what i want to achieve is
local function a()
b()---on this function call, i want it to return "return end" when condition is true and not use if statements to determine the values the b function returned
end
You can use the coroutine.Running() function to get the current running coroutine to then yield/close it
local function Check(n: number)
if n > 0 then coroutine.yield(coroutine.running()) end
end
local function True()
for i = -2, 5 do
print(i) -- -2, -1, 0, 1
Check(i)
end
end
local co = coroutine.create(True)
coroutine.resume(co)
coroutine.close(co)
Unfortunately, this will mean you have to run your code in a separate thread so you will not be able to yield your code while this your function is running
Thanks for the post, it looks like using just functions for this is not possible ;-;, also i think its possible for check function to just return coroutine.yield() without any arguments if u just wanna yield the coroutine co
Well… after a bit of experimentation I did find one way to do it… not sure you’re going to like it though !!
local function b(condition)
if not condition then
error("Check")
end
end
local function a()
b(false)
print("Still going") --> does not print
end
task.spawn(a)
print("Carry on regardless") --> prints
Yes I tried that too, but I guess normally if you purposely throw an error you want it to work immediately.
I tried task.cancel and passing the spawned task but that came up with a weird type error stating the thread I passed wasn’t a thread.
I am trying to stop a function by calling another function without using if statements or coroutines, @Wigglyaa posted an answer thats closest to what i was looking for
I think i found a better way, look the the following code:
function b(condition)
if condition then
condition.awdsad=1
end
end
function a(condition)
b(condition)
print("Exec") --> only prints when condition is false
end
pcall(a,true) --> does not execute statements in "a" function
pcall(a,false)---> executes statements in "a" function
Basically cause any error and use pcall to catch that error, this way there will be no errors shown in output and we can achieve the effect of stopping a function by calling another function