Are events just loops waiting till the action is triggered

I know how events work in a very abstract way, a listener waits till the action is triggered. But is this essentially a loop waiting for the action to get triggered, if it is isn’t this inefficent.

2 Likes

I’m assuming you’re talking about Remote Events and Remote Functions. It’s to communicate client-server. Basically, remotes are fired trough a client, which would communicate with the server, replicating an action trough the server. Why do we use them? Since FE, we have to use remotes to replicate from the client to the server, because back then, everything from the client was replicated trough the server so exploiters could literally do almost anything in the game. If you want to change the color of a part with a gui, you need to use remotes so a specific action can be made, but before, you could do it directly with a local script. Hope this helps.

Uh no Im talking about normal events like the .Touched event. Im asking if theyre basically loops waiting till the event is met

Events are not loops, they are triggered when the event’s action or event is made (my explanation here is weird). Events can be triggered multiple times like if you use the touched event, where it will fire multiple times because you are touching it multiple times. That’s why you can use variables to limit the constant triggering of the event.

Yes I know they are triggered when an action is met, Im talking about how they are implemented in lua

Events and loops are totally different things. One is always doing the same action constantly while the other is doing an action when it’s triggered. Don’t mix them up.

Events are like signals. An example is .Touched as you stated.
Whenever 2 parts intersect, that’s a collision. They’re then both pushed out of each other, and fire .Touched for each part.

Events don’t poll (i.e doing repeat wait() until x)

1 Like

Ok, but how would they trigger. If you need to know if two parts are colliding don’t you need some sort of loop? Since the parts can collide anytime

Well, if they don’t have sort of loop how are they always listening for this touched event to happen?

1 Like

Roblox runs the function when it registers the event (e.g. two things touching). The internals don’t really matter, it probably just checks for whether stuff is touching (or whatever) every time anything moves within a certain range of it, or maybe every frame.

2 Likes

So at the end its a loop constantly checking for collisions? (For the touched event)

Not really, it probably only checks when something somewhere else had changed (Size, Position, etc. of it or nearby parts). It might run those checks after changing certain properties or whatever, I’m not sure. I doubt it just checks every frame or every (insert arbitrary unit of time here), though.

2 Likes

Yah but to check if something changed, dont you need a loop for that to?

Events are triggered when an action is detected, it’s not just in Roblox Lua, it’s in all other languages.

Yes I get that that isn’t my question I know events trigger when an action happened, I am asking how it is actually implemented internally

Probably not, it just would run a check in response to Lua or physics setting a property. The internals don’t matter though, It Just Works :tm:.

2 Likes

It’s implemented deep in the Roblox API then. There isn’t really a way to know because that’s made deep in the Lua code of Roblox.

Well to trigger an action when something is detected, don’t you need some kinda of loop. Not talking about us actually coding, talking about internally

Yah my question is really about how it works, not really how to use events

No, they don’t. The backend Roblox has set up is what allows Lua to change the internal property, since that kind of thing isn’t done purely by Lua. Whatever process is doing that can also call other relevant functions/trigger relevant events.

4 Likes