Does connecting functions create multiple of the same funciton running twice?

Let’s say when I click a button it fires a RemoteEvent, and connects to the server. If I click that button a ton, would that create multiple ‘Run’ functions?

local function Run(player)
     print(1)
end

RemoteEvent.OnServerEvent:Connect(Run)

And more so, if there was something that’d cause a yield, what would happen then?

local function Run(player)
     player:WaitForChild('Backpack') -- Just using Backpack as example of yielding

    print(1)
end

RemoteEvent.OnServerEvent:Connect(Run)

And I clicked the button a bunch. Pretty sure if a function is running, it keeps running until it’s gotten to the end. However, if the function is still running when it is called again, does that mean ‘2 versions’ of the function are running simultaneously, and thus clicking said button a lot would cause several hundreds of functions to possibly be running at the same time?

1 Like

Event listeners are called in a separate thread. I wouldn’t say the function is being copied and the copy is being called per se.

Could functions that require yielding cause problems with memory tho…? :thinking: I’d imagine if the same function is being called 50 times or so at once, problems would arise

No. As mentioned the same function is called multiple times but on a separate thread. The function is not being duplicated.

1 Like

So functions with yields in them wouldn’t cause any lag? :confused:

Please explain what you mean by “lag”.

However, in your use case showed, it won’t cause any delay.
However, if you did something like this:

function waitTest()
wait(2)
end

print("first") -- prints instantly
waitTest()
print("hi") -- prints after 2 seconds

The yield will affect when the prints actually print

1 Like

Well I call a function that then uses :ApplyDescription() which yields, and I’m getting what my friend described as ‘memory leaks’ and causing the client to eventually become near unplayable if they continued. So that’s where I’m at a loss as to why

You need to share some code for us to observe.

Better to just share the thread

Each time an event fires, a new thread is created. This means if I had this code:

local function Test()
    wait(15)
    print("Hello")
end
button.MouseButton1Click:Connect(Test)

And then I spam-clicked the button 20 times in 10 seconds, there would be 20 threads created, each still waiting on its own wait(15) call to finish.

The function itself is not copied in any way, nor does it have any sort of running state. It merely provides instructions that can be run by a thread.

Would that cause a drop in fps tho? As the game is now running a number of threads

No. When a thread ends, it doesn’t do anything else. It is eventually removed from RAM.

To answer your question clearly, yes, each time an event fires, a new instance of your code runs, which means you could have multiple invocations to the same function running at the same time.

1 Like

For some extended information: whenever you use connect, you are actually asking your function to listen for something to happen. Connect method returns a special object, RBXScriptConnection. This holds the function listening to the event you connected to and allows you to disconnect it. Each connection is, as mentioned above, a new instance (literally too) of that function.

In terms of event timing, if that’s of any interest to you, an old thread of mine has an engineer response regarding event connections (title slightly inaccurate):

1 Like