Problems with pcall() and :Connect()

I have encountered a problem when calling Signal:Connect(). If Signal:Connect() is called with any argument other than a function, the script will forcibly error. (Even when wrapped in a pcall / xpcall).

-- Works perfectly fine
pcall(function()
	game.DescendantAdded:Connect(function(instance)
		print(instance)
	end)
end)
-- Returns an error, even when wrapped in a pcall
pcall(function()
	game.DescendantAdded:Connect()
end)

*This happens with any signal, not just game.DescendantAdded.
This may be a studio bug but I put this topic in scripting support in case there is a logical answer to solve this problem.

I wouldn’t say its a bug, But Its sort of something I’m not sure how to explain but I’ll try:

The pcall() in this example is only Connecting an Event, which normally doesnt result in an Error, when an Event fires, It doesnt fire on the Main Thread, but seperately where it will only error for the code its firing, and not where the main code is, its like when you create a Event using RunService.RenderStepped, and an error occurs, it will spam that error, but it doesnt stop the main code.

And when the Event is firing, Its firing from that Event that was Created, and not from the pcall(), you can try placing the pcall() inside the Event if that helps.

2 Likes

I would say it’s a bug. @DasKairo is absolutely correct. The reason why pcall isn’t catching the error, is because the error is happening in another thread entirely. The function actually successfully completes without ever throwing an error in the calling thread. This is deeply problematic; we’d expect the code to stop in these cases.

Instance.new("Part").Changed:Connect()
print("This still prints...")
1 Like

Another thing to add on here, this would also have an error:

pcall(function()
	task.delay(1, function()
		error("example") -- actually fires
	end)
end)

Probably because It would also be in another thread.