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
â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