Question regarding connection optimization and script count

Suppose you run a game with multiple scripts that wouldn’t meld well together, so you keep them separate scripts. However, you hold multiple connections via the same function, say, for example, RunService.Heartbeat. Typically the fewer connections you make the better since that would be fewer RBXConnection Instances that would have to be made… as far as I understand it. So, without being forced to try and combine scripts, would there be any other way to better optimize these connections in any way, or is there merit in having fewer scripts running in your game in general?

Apologies if this is a repeat post. I had trouble finding the answers I’m looking for.

You shouldn’t worry about Connections until you see an actual difference is game quality.

Even having up to 20 .Heartbeat connections are fine as long as the functions aren’t hard performance wise.

Having different scripts isn’t 100% the best performance wise in simple terms, but it depends what you’re doing. But theres also the positive of it being almost 100% more easier to read and code. Also if you encounter an error it will only effect that script and not crash the entire system.

1 Like

There isn’t a strict dichotomy here of either being forced to combine scripts or create less scripts. My main project shards code and data across over 2000 ModuleScripts (excluding sound data which is another ~500+ ModuleScripts) and it performs just as well as any other optimised experience.

In my personal opinion, if you’re able to get a middle ground of some kind then that can help optimise your code. For example, in the case of Heartbeat, I have multiple pipelines that may create a Heartbeat loop but systems of the same pipeline “hook” into a main Heartbeat loop. So when the main Heartbeat fires, it will go through anything I’ve “hooked in”.

-- In one script, I might have:
function System:Run()

-- And in the main Heartbeat script, I may have:
for _, update in ipairs(loopedUpdates) do
    update:Run()
2 Likes