In my case, would CollectionService be more efficient?

I’m thinking of moving client code from running inside of a LocalScript inside of every instance of the tool to running from one LocalScript in StarterPlayerScripts that uses CollectionService to keep track of tools that require the code. Would this be efficient & not cause lag?

both when done right would be O(n) time-wise (and probably space-wise too) assuming you implement both correctly, do whatever’s easiest for you

1 Like

I think it would depend heavily on what the script is actually doing, in any case, utilise the microprofiler and all of Roblox’s stats tools to help you work out the impact.

1 Like

Depends on your implementation and usecase.

For maintainability sake, CollectionService is extremely more maintainable than using scripts everywhere, and unless you have some expensive init function, it takes basically the same speed as a script.

If you’re running without using task/coroutine and instead running it one by one, its probably faster since script startup spawns a new thread for each script.

1 Like

The code for my tools is in OOP, so every time a tool is dropped (into workspace where LocalScripts can’t run) and then picked up (where then LocalScripts can run inside the player’s character), the constructor for that tool is called, so I guess it can be considered that I’m running an “expensive init function” as @metatablecatmaid says.

The reason in the first place that I’m moving the scripts to a LocalScript in StarterPlayerScripts is because I need client tool code to run even when the tool is dropped sometimes.

I was just worried about lag / if one thing in that one LocalScript goes wrong, then it breaks all tools in the game.

Thanks for all of your guys’ insights!

Moving your code from LocalScripts in each tool to one LocalScript in StarterPlayerScripts using CollectionService is likely a good move. It can cut down on the number of scripts running at once, which could make things more efficient and reduce the chance of lag.

A few things to keep in mind:

  1. Less Scripts: Having one script handle everything means you have fewer scripts running, which could improve performance.

  2. Using CollectionService: This is good for tracking tools with tags and shouldn’t cause any issues, as long as you’re not doing anything extra, like re-tagging things constantly.

  3. Handling Events: Just be careful with events like Touched or Activated firing too much, since everything will go through one script.

As long as you don’t go overboard with events or unnecessary checks, it should work fine and run smoother than before.