Any tricks or good practices to optimize Gui scripts?

I ask for some tricks or good practices to optimize gui code, i know that sometimes there is some not know methood or smth to do that, soo i ask you, i pretty much don’t like working on client with Gui, but i have to, thx for help.

1 Like

Im not really sure how to optimise gui code but if you’re tweening a gui, make it a function to use it easier. If that’s something like optimisation.

2 Likes

Ik, i mean more of a how to for example avoid visual bugs, for example i need to place few statements to prevent draging inventory slots from dissapering

1 Like

My opinion would just get the code working then simplify.

2 Likes

This takes a lot of time, i’ve used this way today, but sadly after few hours of scripting code was messy due to visual bugs and a lot of statements, soo i decided to rewrite it and also add comments to entire game’s code, sadly it’s time consuming to rewrite and even more time consuming to fix bad written scripts, it’s better to write it good from the start

1 Like

Components are really powerful for reusable elements, and a good way to organize to fix potential visual bugs later on. An easy library to use would be something like Fusion, or make your own UI component system.

1 Like

When it comes to UI, taking advantage of things like UI hierarchy, properties such as AnchorPoint and Scale, and UI elements such as AspectRatio and UIListLayout can help you avoid lots of lines of code and simplify many scripts. Another trick I like to use is when I want to dynamically create many UI elements, for example an inventory item, I create a “Sample” element and copy that, only modifying the required properties through code. That way I can also save lines that I would normally use to create and customize the item.

1 Like

I’ll keep it simple.

  1. Use 1 function when connection to events. Don’t create many anonymous functions. This doesn’t doesn’t always apply to all events so know when to do it.

  2. Use modules. This won’t optimize your code but it will make it more readable. Don’t have a script that’s too long. Also modules are good if you need to redo the same action throughout different scripts.

  3. If you got the same code pasted all around then just turn it into a function.

  4. Disconnect from unused events.

  5. Clear tables or set specific keys to nil. This is a common way memory leaks happen.

  6. task.wait over wait

1 Like

About unused events, when i should disconnect them? Yk, for example from what i know Character doesn’t break connections when leaving, is this the point with resetOnSpawn too?