How can I error a function with a function inside of it?

So I was curious…

Can you error a function within a function… with another function?

Here I have a function called “curiosity” with a connection called “con1” inside of it. When you send an error through the connection it will error the CONNECTION and not the ENTIRE FUNCTION.

Is there any way to error the entire function rather than the connection?

Within a function you can easily have it cascade.

Within a connection (as is seen within your screenshot) … it’s not so easy.
One possible way is to have the connection toggle a value, which the function is repeatedly checking.

e.g.

local function fizz()
	local err = false
	buzz.Event:Once(function() -- once/connection
		err = true
	end)

	-- option A:
	while (err == false) do
		task.wait()
	end
	error("AAAAAAAH")

	-- option B: 
	while (some other loop) do
		if err then
			error("AAAA")
		end
		...
	end
end

Option A would cause the function to never return unless it errors (unless you add a timeout to it).

If there’s already a loop within the function elsewhere, you could try adding the check into it there (option B).

What you have there is a good example of signal/event oriented programming. More specifically you have an event that handles errors(the Path.Idled event) and a hidden event that handles expected behavior(some block of code underneath).

Something you can do to solve such problems is “chain events”, where you create proxy events that must fire and can be triggered by a range of possible scenarios. Then you call Wait on those events who you know can’t create an infinite yield. Here’s an example of me doing that to connect two events together(the waiting event and the actual event I’m waiting to fire):

For example you can have two events inside the Curiosity function, the error event, and the event that is supposed to run under normal circumstances, and connect them through a proxy event, which you wait for using :Wait(), and then run a supposed function(in this case either error on the upper thread with the error that occurred or the outputs of the function that ran normally).

Here’s how it could look like using event chaining:

local function Curiosity(NPC, Path)
	local proxy = Instance.new("BindableEvent")
	local Con1, expectedBehavior
	Con1 = Path.Idled:Connect(function(C)
		--task.cancel runs just before you fire the proxy
		task.cancel(expectedBehavior)
		proxy:Fire(false, "example error") 
	end)
	expectedBehavior = task.spawn(function()
		task.wait(10) --for the sake of demonstration 
		--disconnect also runs before firing the proxy
		Con1:Disconnect()
		proxy:Fire(true, "example of expected output")
	end)
	local data = {proxy.Event:Wait()}
	proxy:Destroy() --avoid memory leaks
	--error case
	if not data[1] then error(data[2]) end 
	--normal case
	--remove the first parameter that is a flag for indicating the scenario
	table.remove(data, 1)
	--return the rest of the response
	return table.unpack(data)
end

If you want to chain more scenarios together you can use integer flags to differentiate between each of them(0, 1, 2, etc) also you can cache the state of each scenario as it occurs if you want to achieve more complex behavior. For example lets say returning a partially calculated result of expectedBehavior if an error happens to occur before the computation has completed. Lastly you must be certain at least one of these scenarios will fire at some point else Curiosity will yield forever.

spoken like chatgpt

buzz.Event:Wait()
error()

Those are my words.

This ignores the posibility of buzz.Event never firing, which is possible when an error occurs. Also ideally, when an error occurs, you want the function to stop right away, not after another event fires. That’s the infinite yield issue I mentioned above and the reason a proxy event is needed.

Has the same issue as A, where it would continuously yield and prevent the script from progressing. The option A I have in mine allows for some more processing to occuring, such as adding a timeout event by adding a counter and then doing e.g. while (err == false and counter < 10) do.

(Also that is just how Nyrion posts - it’s not ChatGPT)

then task.spawn it whats the issue lol


those loops will run immediately after the event has been connected, the event will have to be triggered IMMEDAITELY in order for those loops to actually do something

If I task.spawn it the error will occur inside the thread of task.spawn, the OP wants the error to occur on the main thread. Basically my solution sends a message one level above to the main thread with an error message and makes that thread error instead(what OP wants).

regardless, your wont work, your event has to be triggered immediately when its connected before the execution reaches the while loops.

local conn = Path.Idled:Connect(function(c)
    local ok, err = pcall(function()
		error("Error inside connection")
    end)

	if not ok then
	   conn:Disconnect()
	   error("Connection failed: " .. err)
	end
end)

The while loops wont yield the code, they are supposed to run(if they exist in the OPs function) inside the expectedBehavior thread, and because it is a thread, it wont yield any code. The yield is done by the proxy on the Wait call. The proxy is what the main thread waits for to realize what’s going on, and both cases that may happen(either the error or the actual code running) fire said proxy.

when did i mention yielding? lol
do u not understand what i meant at all or what?
for your while loops to actually detect the error the event has to be triggered immediately so the err is TRUE when the execution reaches your loops, and you are not guaranteed it will be triggered, hence ur loops are useless

My code doesn’t contain while loops. Maybe you mistake my response with @SeargentAUS response that contains loops? Signals/Events aren’t loops.

Also I assume yielding to be relevant because you mentioned while loops, and one main difference between while loops and events is that while loops yield the code, events do not. That’s because the functions inside events run as if they’re different threads.

Yeah I was talking about the other guys code, you replied to my reply to his code, thats why i was talking about this

So we basically had an entire conversation over different segments of code. Let’s pretend that didn’t happen :skull: