Im not very good at scripting and i wanna ask for any tips on how to optimize code. What should i be doing? only thing i really know to do is use task.wait instead of wait
please tell me more about it. i dont understand what do you mean by “optimizing code”.
The reasons i ask question on forum instead of searching on youtube is because i find it a lot better to learn if im talking to people. I have a harder time learning if its just a video.
like for example, you would normally do “Var += 10” instead of “Var = Var + 10”
or
“Bool = not Bool” instead of
If bool == true then
bool = false
else
bool = true
end
Im asking if theres anymore optimizations like this, to help my game run better or just have the code simply look better.
Optimizing your code is helpful for avoiding lag and assuring that your game has a good user experience.
Here are some tips:
- 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.
- Avoid Memory-Leaks → Garbage Collection and Memory Leaks in Roblox - What you should know
- 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.
Heres what i do form that list:
- I dont use chatgpt or youtube
- Could you explain (not sure what that is)
- I try to as much as i can
- I do that (actually find it satisfying too)
- I try as much as i can aswell
Recently i have actually been trying to use localscripts more, i know it is better to run things on the client then server and ive been using that more and more when possible
There is not really any ‘do this and your code will be optimized’.
There is also a difference between optimizing and organizing code. Having organized code is often more important than having the most optimal code.
Only in certain instances is it important to have a very optimized code. For example if the code is running every frame or similar. Often it’s more important to have clean and organized code.
Use modules and function to organize code.
Don’t have a single function do to many things but try to have a function do a specific task instead of many. This will make the code more readable and re-usable by other functions and modules.
I have also been using modules a lot more ever since i learned about them
If you want, i could write an example script, and you could review it.
There is a section on the forum called Code Review
You could try and post some of your code there. I think you will have a higher chance of getting help with your code there.
ok, ill possibly post on there later. Thanks for telling me
There is no difference when using var += 1
instead of var = var + 1
. The bytecode that gets generated is the exact same.
Here are some general performance tips:
- Always use local functions whenever possible. The only time you ever need a global function is if it’s part of a table you want accessible outside the module. Use local functions for private methods and recursion. Local functions can get inlined which is a huge performance gain.
- If you have a large array of some class or table, split it into multiple large arrays for each field. Look up “structure of arrays”.
- Use Vector3 instead of Vector2 since Vector3 is not heap allocated
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.