How can I break/return function using another function

As the title says, I want to break/return a function using another function.
In case you don’t understand you can check the example below

Example
local function f1()
  --body of the function
  game:GetService("UserInputService").InputEnded:Connect(function(v1)
    if v1.KeyCode == Enum.KeyCode.E then
      return
    end
  end)
end

I want the function f1() to end/break/return when player clicks E

Any help would be appreciated :slight_smile:

3 Likes

I do not know how this could be done, but you might be able to make an if statement in the function you want to break that has a Boolean variable then just change the value form the other function to false, making the other function stop.

I mean technically you can do this:

function C0()
return 1 + 2
end

function C1()
C0() -- Other function
end

I could have done that, but in the game player keeps on pressing E and the the bool variable dosen’t work there… like I have tried that and it won’t work in my case… I want the function to end immediately when player clicks E…

umm that’s not exactly what im looking for, thanks anyways :slight_smile:

I did not say you had to create one like that, do it like this:

local stopFunction = false

and put it at the top of your code.

ig you are talking about debounce right ?

pretty much, if you want to call it that.

yea, that won’t work in my case…

Im sorry, Wish there was more I could do to help but that is all I can think of.

Incase you don't understand what I mean
local debounce = false

function one()
       if debounce == false then
              -- code --
       end
end

function two()
       debounce = true
end

Another event, Events use :Connect and functions are just function x()


local function f1()
  local done = false
  --body of the function
  local event
event = game:GetService("UserInputService").InputEnded:Connect(function(v1)
    if v1.KeyCode == Enum.KeyCode.E then
      done = true
      event:Disconnect()
    end
  end)

  repeat task.wait() until done
  return
end

Add debounces like @Waffle_Gamer6 said and make sure to disconnect the event after called to avoid memory leaking.

2 Likes

This is the best response. We cannot return out of a function from another connection unless by “break/return” you mean “stop looping”. It would be more useful to us if you posted the full function you hope to break out of.

1 Like