Are Events Asynchronous?

UIS.InputBegan:Connect()
If m1 click then
Task.wait(10)
Print(“One”)
Elseif E then
Print(“Two”)

If e is pressed less then 10s after m1 click, what will happen? Will it wait until the code for m1 to end, stop the m1 code, or run asynchronously?

I asked the same question specifically about UIS, and people said it will wait until code for the previous input ends. But if :Connect() runs code asynchronously it should run at the same time.

I’m sorry for the simple question. I can’t test it out in studio cuz I can’t use my computer rn & its not on documentation or devforum so I thought the info should be put on the internet

No, it won’t wait until the m1 is finished

1 Like

Each RBXScriptConnection callback will spawn in a separate thread and even reuse the same thread if the callback doesn’t yield. If it does yield, then it just creates a new thread.

Signal.Event:Connect(function()
	-- This always prints the same thread
	print("First: ", coroutine.running())
end)

Signal.Event:Connect(function()
	-- This uses a different thread than the first
	-- but it will still reuse it
	print("Second: ", coroutine.running())
end)

Signal.Event:Connect(function()
	-- This also uses a different thread
	-- however, it won't reuse it because we yielded
	print("Third: ", coroutine.running())
	task.wait(0.5)
end)

Signal:Fire()
Signal:Fire()

It’s good practice to avoid yielding in an event callback because of this. Even if the callback resumes before the next invocation, the engine can’t safely reuse it.

2 Likes

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