How to break out of an outer function

How to break a function while inside a function?

game.Players.PlayerAdded:Connect(function ( )
    part.touched:Connect(function ()
		print("returned")
		return
	end)
end

How would you break the outer function playerAdded with the touched function?

I want this to be instant, no loops checking it

I do not want to use a while wait(0.001) loop or any high costly loops.

1 Like

Just return the outer one as well.

1 Like

u can just assign a variable touched and break the loop when its true

local function epic ( )
	local touched = false
	part.touched:Connect(function ()
		touched = true
		print("returned")
		return
	end)
	while wait(0.1) do
		if touched == true then
			break
		end
		print("epic")
	end
end
1 Like

sorry, I miswrote the question, i edited it to be more shaped to how I wanted it

sorry, i reworded the code and question to get the answer I need

Create a book value for if it is touched or not and once it’s touched set it to true. And then below that you check if it is true then break out of the function.

:Disconnect() is what you’re looking for.

local lol
game:GetService("Players").PlayerAdded:Connect(function(Player)
	lol = Part.Touched:Connect(function()
		print("hi "..Player.Name)
		lol:Disconnect()
	end)
end)
1 Like

did not think of that. very nice!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.