How do events in UserInputService work? (Events in Events)

Example:

UserInputService:Connect()
if E then
(run code)

part.Touched:Connect(function()
--vfx etc
end

end

Every time an event happens, it runs the code inside parallel to the rest of the script until the code completes. If theres an event inside the event, such as Touched inside UserInputService, it waits until Touched fires and runs the Touched code. After it ends, does the parallel code still exist or does it stop.

(if E is pressed 10 times, on the 10th time will 10 touched events fire)

Events run in some sort of parallel, but you genuinely don’t want events in events. Only if the (first) events run like once or twice per server, but you don’t want to put a .Touched in a UIS key event

1 Like

@Octav, you are totally on the right track about how events in Roblox work!

When you connect a function to an event like UserInputService, each time the event fires it runs the connected function independently of the rest of the script so it doesnt block anything else. If you put another event connection inside that function like a part.Touched it doesnt automatically multiply or persist in the way you might expect.

Each time the outer event fires it will create a new connection to the inner event which can quickly get messy and lead to multiple responses all running at once!! (especially if the outer event happens frequently) Once the inner event fires and its function completes, that particular execution is done but the connection itself remains active until explicitly disconnected, so repeated outer events will keep adding more listeners if your not careful.

Its fine for occasional use or one-off events, but nesting events inside events generally leads to redundant connections and unexpected behavior which is why its better to connect events at the top level or manage them carefully with flags or disconnect codes.

In short the code inside the event runs in parallel but the connections themselves persist and can accumulate if your nesting them unnecessarily!!!

1 Like

Yeah I’ve.. been coding since 2021…

1 Like

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