Do events create new threads each time they are fired?

The title is quite self explanatory, when an event is fired, does it create a new thread? I mean if let’s say 2 events are fired from 2 different players to the server then will it create 2 different threads for each player which will be running separately at the same time?

2 Likes

I believe so if I’m not mistaken. If they were on the same thread when being fired, then

part.Touched:Connect(function()
    print("e")
    wait(10)
    print("e2")
end)

would yield for 10 seconds before it could be used again, but it doesn’t if you try it out, as if the wait isn’t there until you notice that after 10 seconds it prints “e2”, which does sort of indicate that they’re on a new thread for each time the event has been fired.

tl;dr: Yes, I believe that a new thread for everytime the event has been fired because if it wasn’t, then there was be clashes if the event is fired multiple times/has waits in it

6 Likes

Your method worked, thank you for clearing my question!

4 Likes

Anytime! If you have anymore issues don’t e afraid to make another post!

2 Likes

Just want to expand on what @EmbatTheHybrid said a bit.

Roblox never runs things “at the same time”. (At least until they fully release parallel lua, but that’s a different story). It will continue running the same block of code until it reaches a yielding function like wait or it reaches the end. Afterwards, it picks another block of code to run.

This job of picking which code to run is done in part by the Task Scheduler | Roblox Creator Documentation.

For example, say you had two event handlers, one for Touched and one for Mouse.Button1Down:

workspace.Part.Touched:Connect(function()
  print("a")
  print("b")
  print("c")
end)

game.Players.LocalPlayer:GetMouse().Button1Down:Connect(function()
  print("d")
  print("e")
  print("f")
end)

What happens if you click the mouse on the exact same frame that you touch the part? What shows up in the output?

Well, because roblox only runs one thing at a time, these two handlers won’t be interleaved like a d b e c f. Instead, you’ll either get a b c d e f or d e f a b c, because one handler will finish running completely before the next one starts.

If you add yields between all of those prints:

workspace.Part.Touched:Connect(function()
  print("a")
  wait()
  print("b")
  wait()
  print("c")
end)

game.Players.LocalPlayer:GetMouse().Button1Down:Connect(function()
  print("d")
  wait()
  print("e")
  wait()
  print("f")
end)

Now you could get interleaved outputs like d a b e c f or something.


P.S. This is why, in the ROBLOX docs, they’re so careful about telling you if a function is yielding or not. Take UnionAsync for example:

Yields

This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts.

That’s really important info to know, because if your code relies on some external state (variables, parts in workspace, etc.), that state might change as soon as you call a yielding function. Things might look very different on the following line!

9 Likes