What is the best way to make you script shorter

I know in other platforms like JavaScript have prefixes so you don’t need to put like the! before each admin command if your making it is this possible

1 Like

I don’t know much about JavaScript (would totally love to learn more about it sometime though!), but I don’t think making a script as short as you possibly can while increasing the risk of making it more difficult to work with, but it is worth it to take some optimizations to not just make it easier to read but also shorten the amount of lines, while still having your code clean.

You can use ModuleScripts for reusable code that you plan use across multiple scripts in your place which can help make it easier to understand what is going on in your main script, and that does come with your script becoming shorter and can even look pretty nice with custom constructor and method functions.

You can also use loops for certain repetitive actions in your code to shorten the amount of separate identical actions you have to write yourself.

TextLabel.Text = "Loading Gui"
wait(1/3)
TextLabel.Text = "Loading Gui."
wait(1/3)
TextLabel.Text = "Loading Gui.."
wait(1/3)
TextLabel.Text = "Loading Gui..."
wait(1/3)

Now by using loops, you can shorten this code to only a few lines:

for i = 0, 3 do
    TextLabel.Text = "Loading Gui".. string.rep(".", i)
    wait(1/3)
end
1 Like

Don’t really know if you’re talking about this, but here’s a video about Roblox TypeScript made by sleitnick.

And here’s another video also made by sleitnick talking about Code Snippets for Lua.

I think you’re asking the wrong question. One of the most important questions and the one that every programmer has is how to make the code more readable. Because there always comes a time when you have to read that code again and modify or add more things. If the code is long and disordered that task can become very difficult. But writing less code is not the solution. It is like when, with difficulty, we manage to put all the suitcases in the car and it turns out that at home there are some more. The only way to get everything in is to take all the suitcases out of the car and start again. It’s the same with the code. In the end you will end up rewriting everything again when you want to make some changes.

Another mistake that is usually made is, to do implicit things to write less code. The problem is that with time we forget the context in which we wrote those implicit things and it becomes impossible to read the code. In Python language we have a motto “Explicit is better than implicit”.

To summarize, writing readable code is much more important than writing less code.

Python koans are so beautiful that I can’t stop publishing them

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea – let’s do more of those!

4 Likes