Do two connections run parrallel?

Yo Yo Yo People!!!

soooo…

dosomething:Connect(function()
end)

dosomething2:Connect(function()
end)

My question would be:
if they both activate at the exact same time, will they run in parrallel or will just one run and the other one stop running?

orrrr uhm will one run and then the other one?

They actually run sequentially not in parallel also the order depends on which was conncted first That’s why each connection runs to completion before the next starts , if you really want true parallelism use the task library

local event = Instance.new("BindableEvent")

event.Event:Connect(function()
    task.spawn(function()
        print("First")
        task.wait(1)
        print("End First")
    end)
end)

event.Event:Connect(function()
    task.spawn(function()
        print("Second")
        task.wait(1)
        print("End Second")
    end)
end)

event:Fire()

This would allow both functions to run concurrently

The task library is not true parallelism. They are still just coroutines. Luau is a single-threaded language. Everything runs in serial. The events already run in their own coroutine threads, so spawning another within the connection is pointless.

If you want true parallelism, you need to use Actors, which run in separate Luau VMs, and allow you to “desynchronize” a block of code from the main thread.

See: Parallel Luau | Documentation - Roblox Creator Hub

4 Likes

so… if they both get activated at the same time , will one stop running? :melting_face:

They don’t technically get activated at the same time. Only one runs at a time, until it yields. Once it yields, the other one can run.

so they can’t run at the same time? like if 2 values change at the same time i guess, will one connection break?

There’s a difference between multiple threads and parallel threads. Multiple threads can run on one CPU core, while parallel threads run on different CPU cores.


Oversimplification warning!


Think about it like a group of friends working on a bunch of tasks. Each friend represents a CPU core. Now, there’s also a bucket full of tasks. Each task represents a possible thread.

Multithreading (having multiple “connections” running at the same time on one core) is like one friend choosing to juggle multiple tasks at once. That friend will work on one task until they’re done the task then move on to the next.

Parallel threading (having multiple “connections” running at the same time on multiple cores) is like multiple friends completing different tasks all at once.

Now let’s say we had 100 tasks and each task takes 1 second to complete. If we only make one friend do it, it will take 100 seconds.
However, if we split those tasks equally among the 4 friends, each friend only does 25 tasks so completing 100 tasks will only take 25 seconds.


In your case, your two connections are two threads running on the same CPU. You aren’t giving tasks to any other core. CPUs run billions of times per second which makes operations extremely fast and so it seems like it runs at the same time. In reality though, it’s running each connection one after the other.

what happens if i have like

dosomething:Connect(function(1)
end)

dosomething:Connect(function(2)
end)

when the script reads the first thing and it gets activated before it read the other connection? :open_mouth:

BTW thanks for the explanation!! :heart:
so also if two things like change at the same time, none of the connections wont run?
thanks

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