How should i optimize code?

Optimizing your code is helpful for avoiding lag and assuring that your game has a good user experience.
Here are some tips:

  1. If it is possible, always make code on localscripts. Try avoiding making server-sided scripts if you could make it localscript. For example: A windmill its blades should rotate. The best option is to make it with a localscript, because if you make a server-sided script, the computation will be done on the server, causing more lag.
  2. Avoid Memory-Leaks → Garbage Collection and Memory Leaks in Roblox - What you should know
  3. Make sure to avoid loops as much as possible. Make use of events instead of loops.

If you want your code to look good, I suggest looking into functional programming. I use this way of coding all the time and it makes the code look allot more simpler.

2 Likes