Stopping a function that is inside another function

So basically i have a function inside another function lets call Them function1 and function2, How would i stop both of function2 and function1
Like the code below

local function Function1()

  local function Function2()
      If Condition == true then
       --Stop The functions here
      end

  end
--Doesnt print When both functions are stopped
print("Still Running")
end

I tried using return but it doesnt work, I looked everywhere on devforum but everything seems complex Is there anyway easier than doing all of that complex stuff? Just like return

4 Likes

The only way you can do this is by using Coroutines. They are simple and can be used to stop or resume a function

1 Like

Use return to stop the function.

Example:

local function Function1()

  local function Function2()
      If Condition == true then
       return
      end

  end
  return
--Doesnt print When both functions are stopped
print("Still Running")
end

Can you provide an example of how to implement it?

He just said return doesn’t work…

Tried it before posting doesnt work unfortunately

Ah I see - I don’t fully understand the question. Could you give content please?

Not really sure how i can explain it in a more simple way but what i mean when stopping function2 like return or something it affects function1 too in a way that it doesnt keep running I Hope you understand what i mean

You can view this for better information, but simply

local printHello = coroutine.create(function()
    print("Hello, world!")
end)

Coroutines are basically like functions that can be stopped. You can either put the entire function in the create function or just reference the function. The coroutine can be started using coroutine.resume( function name here )

hold on let me try to write some code wait a sec

Ah, I understand now. Here is some code:

local Condition = true
local function Function1()

  local function Function2()
      if Condition == true then
         return true -- If condition is met then return true
      end
  end

  if Function2() == true then -- If the callback from Function2() is true
    print("Still Running")
  else
    print("Stopped Running!")
 end
--Doesnt print When both functions are stopped
end
Function1() -- Start the first function

Here you go, I’ve updated this code so it should now work depending on the condition.
It works by returning a true or false value and if the value is X then it will continue in Function1.

Doing this in your way is quite annoying especially when you have alot of functions

Maybe don’t use functions and just use if statements instead as you dont really need a function anyway.

I tried doing it using coroutines but it doesnt work because as i mentioned in the example function2 is inside function1 and function1 cant be accessed through function2 thats why it wont work i hope i this isnt confusing

For my game i need it cause it contains events and events needs functions

Instead of this:

local Condition = true
local function Function1()

  local function Function2()
      if Condition == true then
         return true -- If condition is met then return true
      end
  end

  if Function2() == true then -- If the callback from Function2() is true
    print("Still Running")
  else
    print("Stopped Running!")
 end
--Doesnt print When both functions are stopped
end
Function1() -- Start the first function

Do this:

local Condition = true
local function Function1()
  if Condition == true then -- If the Condition is true
    print("Still Running")
  else
    print("Stopped Running!")
  end
--Doesnt print When both functions are stopped
end
Function1() -- Start the first function

ok let me show you something similiar to what my script looks like:

local function OnServerEvent()
Event1:Connect(function(Condition)
  if Condition == true then
  --End OnServerEvent Function
end)

Event2:Connect(function(Condition)
   if Condition == true then
  --End OnServerEvent Function
end)

Event3:Connect(function(Condition)
  if Condition == true then
  --End OnServerEvent Function
  end
end)


end)

So the condition is inside the event thats why this doesnt work

Ok, I have an idea for you:

local function OnServerEvent()
    local shouldEnd = false; -- Flag to prevent multiple triggers
    Event1:Connect(function(Condition)
        if Condition == true and not shouldEnd then
            shouldEnd = true; -- Set flag to end OnServerEvent
            return; -- Stop execution for Event1
        end;
    end);
    Event2:Connect(function(Condition)
        if Condition == true and not shouldEnd then
            shouldEnd = true; -- Set flag to end OnServerEvent
            return; -- Stop execution for Event2
        end;
    end);
    Event3:Connect(function(Condition)
        if Condition == true and not shouldEnd then
            shouldEnd = true; -- Set flag to end OnServerEvent
            return; -- Stop execution for Event3
        end;
    end);
end;
OnServerEvent(); -- Call OnServerEvent function

Give this a go and let me know if it works for you!

one question why i see this ; everywhere?

I just like to add them, it doesn’t matter you can remove them if you want. (I started with Java and C)