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
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 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…
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.
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.