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?
Could functions that require yielding cause problems with memory tho…? I’d imagine if the same function is being called 50 times or so at once, problems would arise
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
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.
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):