How do I exit from a function from a function inside it?

So the title explains it all, I am trying to exit out from a function from a function inside it.

Example:

function DisconectMe()

LeaveFunction.MouseButton1Click:Connect(function()
-- How do I exit out from the function that is called DisconectMe()?
end)
end
event = LeaveFunction.MouseButton1Click:Connect(function()
    --do stuff
    event:Disconnect()
end)

No, I would like to disconnect from the first function that is called DisconectMe.

function DisconectMe()
    event:Disconnect()
end

event = LeaveFunction.MouseButton1Click:Connect(function()
    --do stuff
    DisconnectMe()
end)

That doesn’t work…

module.Test = function()
 

Button.MouseButton1Click:Connect(function()
		module.CFrameSettings():Disconnect()
end)
end

Bumping…

‘exit’ is not a term in Lua. Do you mean returning early, or maybe ending a loop?

local function outer()
    local running = true
    LeaveFunction.MouseButton1Click:Connect(function()
        running = false
    end)
    while running do
        print("AYOOO", tick())
        wait()
    end
end

Basically exiting the Disconnect me function when the Button gets clicked.

function DisconectMe()

LeaveFunction.MouseButton1Click:Connect(function()

end)
end

Something like this?

function DisconectMe()
    LeaveFunction.MouseButton1Click:Wait()
end

No, no no no, I want to Disconnect from the Disconnect me function when they Press the Button not wait until it’s clicked.

You can’t disconnect that function because you never connect it to any event in the first place.

You can’t disconnect from a function at all. You “connect functions to events” and “disconnect connections” (the objects that get returned when you call Connect).

Sorry I’m not trying to be pedantic but it’s hard to understand what you mean and it’s made harder by not using precise terms

It’s a function inside a function there is nothing wrong, I need the params from that function.

The problem I have is that the function runs twice when the settings function called the Button Clicked function plays twice instead of 1 (That’s why I’ve to disconnect) I’m 100% there is a hacky way returning from the function.

Do you want the button click handler to disconnect itself when it runs? You can do that like this:

local connection
connection = Button.MouseButton1Click:Connect(function()
    connection:Disconnect()
end)

No, I just want to return from the main function when they press the Button (Button function must be inside of the main function).

DisconnectMe is the “main function”, right? If you just do this:

function DisconectMe()
    LeaveFunction.MouseButton1Click:Connect(function()
    end)
end

then DisconnectMe returns immediately. The only way to get DisconnectMe to “return when they press the button” is to wait for that, and that’s what I thought you meant when I made this reply: How do I exit from a function from a function inside it? - #9 by ThanksRoBama

Something like this?

local function DisconnectMe()
	script.Parent.MouseButton1Click:Wait()
	print("EXIT")
	return
end

DisconnectMe()

This will wait till the button is clicked and then exit the function.

Also, if you want something to run when the button is clicked:

local function DisconnectMe()
	script.Parent.MouseButton1Click:Connect(function()
		print("Clicked")
	end)
	script.Parent.MouseButton1Click:Wait()
	print("EXIT")
	return
end

DisconnectMe()
1 Like

I’m not sure, it’s still firing 2 times Can you check my code? (It fires 2 times in the Button loop that has a print statement called “Fired 2 Times”)

function module.CFrameSettings(Frame,CutsceneSettingsFrame, Widget, CurrentCFrame)

		Frame.Parent = nil
		module.SetParent(CutsceneSettingsFrame, Widget)


		for _, Button in ipairs(CutsceneSettingsFrame.EnumEasingStyleFrame:GetChildren()) do
			if Button:IsA("TextButton") then
				for _, Child in ipairs(ResultTable) do
					if Child["EasingStyle"] and Child["EasingStyle"] == Button.EnumName.Text then
						Button.BackgroundColor3 = Color3.new(0.121569, 0.121569, 0.121569)
					else
						Button.BackgroundColor3 = Color3.new(0.156863, 0.156863, 0.156863)
					end
				end
			end
		end

		CutsceneSettingsFrame.TimeDurationBox:GetPropertyChangedSignal("Text"):Connect(function()
			for _, Child in ipairs(ResultTable) do
				if Child["Position"] and Child["Position"] == CurrentCFrame  then
					Child["Time"] = tonumber(CutsceneSettingsFrame.TimeDurationBox.Text)
					break
				end      
			end
		end)

		for _, Button in ipairs(CutsceneSettingsFrame.EnumEasingStyleFrame:GetChildren()) do
			if Button:IsA("TextButton") then

			Button.MouseButton1Click:Connect(function()
				print("Fired 2 times") -- fired 2 times
					for _, OtherButton in ipairs(CutsceneSettingsFrame.EnumEasingStyleFrame:GetChildren()) do
						if OtherButton:IsA("TextButton") then
							local BackTween = TweenService:Create(OtherButton, TweenInfo.new(.1), {BackgroundColor3 = Color3.new(0.168627, 0.168627, 0.168627)})
							BackTween:Play()
						end
					end
					local ClickedTween = TweenService:Create(Button, TweenInfo.new(.1), {BackgroundColor3= Color3.new(0.0823529, 0.0823529, 0.0823529)})
					ClickedTween:Play()

					for _, Child in ipairs(ResultTable) do
						if Child["Position"] and Child["Position"] == CurrentCFrame  then
							Child["EasingStyle"] = Button.EnumName.Text
							break
						end      
					end
				end)
			end
		end



	
	CutsceneSettingsFrame.SaveButton.MouseButton1Click:Wait()
	CutsceneSettingsFrame.Parent = nil 
	module.SetParent(Frame, Widget)
	warn("....")
	return print("XD")
end

you cant. instead, you have to make a bool value and whenever you want to disconnect it, set it to true, then use an if statement to check if the bool is set to true.

3 Likes