I’m making a game and i’m making it so the humanoid of NPCs turn into ragdolls when they dies through .Died:Connect(). The thing is i’m also making the humanoid do other functions when it dies but in separate :Connect() in separate scripts, would it be really bad for performance if i connect the same event many times and by consequence making it do a function like 3 times every time it fires? Even if this .Died event is only used once, i’m trying to make it as optimized as possible, considering this is for NPCs that will spawn a MASSIVE ammount of times. Just to clear out it is possible for me to make this all into one function, but it’d just be more organized visually, so this is mostly just for visual comodity.
Indeed, unavoidably every connection takes up a part of the available processing resources.
The general recommendation is to make as fewer connections as possible, while not excessively sacrificing readability (if you have multiple connections to the same event in many different scripts, then probably you took a more pro-readability approach): In that case, I would suggest that you perhaps consider merging some of these scripts into the same one to reduce the net amount of connections.
Another approach to consider, if you’re in the constant need of making new connections to the same event, is having a script that handles the event, and fires a Bindable/RemoteEvent every time this particular event needs is fired. This way, you achieve that subsequent scripts remain connected to one event, and just one script to re-connect to a connection (reducing processing impact greatly in case you have massive amounts of re-connections happening at the same time).
Let me know if that results of use, hope it does.
Main thing that was worrying me the most about merging it all in one event:Connect() is the fact that i’d have to use spawn()
or task.spawn()
, since it has a mix of many wait()
s through the different functions, and i’m not too aware of how performance-heavy those can be. Do you know about that? If so please tell me about it!
If you’re running code in other scripts, then it means they’re running already in different threads;
If you merge them into the same script, and do task.spawn()
on them, you’re creating a thread for each… so it shouldn’t be much of an impact in performance because it’s the same: they’re already running in different threads.
Alright i got it, thanks a lot!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.