Any tips on how to improve code efficiency?

I would really like to improve the perfomance of my creations since my first game was horrible in terms of perfomance.
I think the issue is with the way I write my code. Could I get any tips/tricks on how to improve code efficiency?
For example: does using a module for all client variables/functions use less memory than writing down variables again and again in each script? Does using more scripts affect game perfomance?
I have searched on the wiki and I haven’t seen any thread about this topic.

1 Like

It’s kind of hard to recommend good practices in general when we don’t know what your code looks like. If you gave us a general structure of some specific game systems and how you structure everything it would help a lot. In the case of you wanting advice on specific code however you should move to #development-support:requests-for-code-feedback

3 Likes

The only thing I can say for now is … any code that doesn’t need consistently running, use a ModuleScript to store it. That itself makes your code efficient performance wise.

1 Like

It doesn’t matter where your code is stored for the performance aspect, it’s whether it’s being used at all.

As for OP, I suggest diagnosing and finding bottlenecks and optimizing those, and also think about how your code uses the Roblox api and if you’re calling expensive stuff often and not using commonplace optimizations like memoizing or lookup tables.

6 Likes

Usually, if you want to optimize your code, you need to take a look at any loops you have.
Make sure you store in a local variable outside the loop an information that’s used in the loop and that doesn’t change. (For exemple a math equation.)

There’s also the Microprofiler

Hey, I’d recommend taking a look at this great article by @Ugh_Lily which contains information about improving efficiency and the best methods to use.

4 Likes

My advice in this would be a few things like before writing a system in a game, write it down on paper and plan it out so you can think of the best way of doing something. Don’t worry about trying to have the “best” script either, just try in the best of your abilities to make something and improve on it over time, ask for help as well to see where you can improve. As well, don’t over optimize something until it works, then you should optimize if needed. Don’t make variables that you don’t use multiple times. The usage of variables may not be a huge impact though I would like to you to expand on what you mean by “horrible in terms of performance”, like is the client lagging or the server lagging? What are you trying to do, etc.?

Thank you for that thread! I find it very useful. For example i found out that I shouldn’t use iterators as frequently as I did before…