How does event Handlers run?

when i call an event handler like .touched do they run on paralelle or what? since i can run the script below it at the same time even when it gets called

3 Likes

You never supplied any code for us.

1 Like

read what i said
“i can run the script below it” “it” is refered to the .touched:Connect(function() end) – thats just an example

3 Likes

I don’t really understand the question, if you’re asking if the code can run after connecting an event, yes it can. You’re just connecting an event, and the function just waits to be called.

Please specify about what your asking, because I had a hard time understanding.

2 Likes

Kind of yeah, they just make a new thread that handles the event.

If you run a .Touched. it doesn’t stop other code thats running if thats what your asking

OP is asking that do such listeners like RunService.RenderStepped or BasePart.Touched run multi-threaded or serial? Because they seem to let the code after the assignment of the event run so OP thinks they run multi-threaded but OP would like to be corrected if they are wrong or assured if they are right.

2 Likes

This is known as asynchronous programming. When you “connect” a function to an event, you’re providing a callback function that is stored by the object for when it triggers that event. In simple words, “Hey, object, take this function that I’m giving you and run it when this event happens”.

This is why you can run code synchronously right after connecting an event, because the code that connects the event is synchronous (lines of code run one after the other with no delay), but the event callback itself is asynchronous (runs at different times. i.e. when the event is triggered).

By default event callbacks do not run in parallel. Your code doesn’t technically run at the same time, but rather the task scheduler coordinates when to run callbacks connected to events and when to run scripts normally every frame. All of this happens in series; no two lines of code run at the same moment unless you explicitly use parallel lua.

1 Like

sorry for thr late reply but yeah thats what i ve vern wondering if u call a touch event while another pary of script is running they both run at the same time

why do events like .touched run at the same time with the rest of the script without yelding it when they are called is it supposed to be serial?

They are made asynchronous by design, if you were connecting multiple remote events in one server script, it wouldn’t make sense that one has to trigger before the next one can work, it would be problematic if the script had to wait each time you set up an event.

Basically, would be more problematic, and would require more effort.

Events in Roblox are implemented as RBXScriptSignal objects. These objects have a “Connect” method, which takes a function reference. The only thing the method does is store the function in an internal array. Because the method contains no yielding code, the code after the method call is able to run. When it comes to the execution of the function you gave to the event, the function is not executed in the same script. At some point in Roblox’s physics engine, when Roblox calculates two BaseParts have collided, it will access the RBXScriptSignal “Touched” events and fire them with the other part. This action has the RBXScriptSignal object travel through its array of stored functions and execute them with the relevant arguments. The functions are executed asynchronously, which you can imagine as having been called one-by-one with task.spawn. This is what allows them to not interrupt other code execution.

Roblox is, by default, cooperatively multitasked. This means only one thread of execution is running at any given time. It is only when a thread yields (pauses) that another may begin running. This is why you need to yield in long-winded loops as not doing so will cause other vital threads to stop running, ultimately hanging the engine on your loop. This is what @pullman45 meant by your code not “technically” running at the same time… at some point, your event handler (connected function) yields or finishes, which lets other code run. Humans exhibit cooperative multitasking daily. Say, it makes a whole lot more sense to spend your time doing something else while waiting for food to heat in the microwave. However, you cannot truly be in two places at once—you need two humans for that. CPUs are divided into a collection of smaller CPUs, called “cores”. Real simultaneous code execution is permitted through splitting work over these cores as its the difference between two humands and one. Roblox lets us tap into this with parallel lua. You can connect event handlers in parallel with RBXScriptSignal:ConnectParallel

1 Like

just a confirmation to this explanation before i go to sleep since i cant concetrate well ( i ve read it like 3 times). @JAcoboiskaka1121 does this look right to you?

Yeah, looks right to me.

theLIMIT

i dont understand clearly if by any chance u were talking about setting an event without calling then no i ve been talking about calling an event should stop the rest of the script until the event function ends no? + i dont actually understand something if luau is a single threaded language how does it have the ability to do asynchronize stuffs?

Luau isn’t exactly a single-threaded language. While it runs line by line by default, it can also handle asynchronous operations and multitasking using things like coroutines and events. For example, when you connect an event like .Touched, the script doesn’t stop and wait for that event’s function to finish. The rest of your script keeps running, even while the event handler is still doing its thing.

Think of it like a food truck with one cashier. If they handle all the customers one at a time, everyone waits in line, and it’s slow. But if you add more cashiers (like handlers running independently), things move faster because they don’t block each other. In Luau, event handlers and coroutines are like those extra cashiers—they let things run separately without holding up the whole operation.

1 Like

then it works in parellel or am i mixing things up?

If you’re talking about Parallel Lua, that’s different from asynchronous operations. Parallel Lua splits tasks across multiple cores, allowing for true parallel execution by distributing workload.

However, in Luau, when you use asynchronous operations like coroutines or event handlers, they don’t run in parallel in the same way. Instead, they create separate threads that operate independently, letting the main script continue without waiting. So, while it’s not true parallel execution, it achieves a similar effect by keeping things moving without blocking the script.

1 Like

whats the differenece between multithreading (or new threads) and paralelle and what do most of devs use ?

Remember that coroutines tie into the cooperative multitasking pipeline. Saying this “keeps things moving” is too vague, and I say this isn’t true. As I stated earlier, in this pipeline, no two threads of execution are running at the same time. Only one part of one script is being executed. It is only when that part yields or the execution of that part is complete (e.g. the end of a function) that another thread may begin running. Think back to my earlier analogy with heating food in a microwave. This task leaves us with nothing to do but wait, so while we wait, we remain productive by doing something else. Luau is doing this at such a high speed that it appears as if code is running simultaneously. Despite this, the choice to spend downtime doing something else is beneficial and does decrease processing time.

You will end up using coroutines more than parallel Luau. Any call to a yielding function counts as a interaction with coroutines. Events involve coroutines, TweenService involves coroutines, etc. The moral of the story is that a large part of the Roblox Studio API and engine revolves around coroutines