Make a code neat and tidy

Let’s say I made a TextLabel. I want the text to change every two seconds, below is the script:

    script.Parent.Text = "Hello"
    wait(2)
    script.Parent.Text = "Hello2"
    wait(2)
    script.Parent.Text = "Hello3"
    wait(2)
    script.Parent.Text = "Hello4"
    wait(2)
    script.Parent.Text = "Hello5"
    wait(2)
    script.Parent.Text = "Hello6"
    wait(2)
    script.Parent.Text = "Hello7"
    wait(2)
    script.Parent.Text = "Hello8"
    wait(2)
    script.Parent.Text = "Hello9"

So that was what I created with my existing knowledge. How can I make the code better and looks tidier?
Thanks in advance.

Instead of doing that, you can do a simple for loop.

for i = 1, 10 do
script.Parent.Text = "Hello" .. i
end

However, that’s pretty much only the general idea. If you’d like to put in custom texts that doesn’t involve in increasing numerals, you can create a table and loop through the table.

local text = {"Hey!", "How", "are", "you?"}

for _,v in pairs(text) do
script.Parent.Text = v
wait(2)
end
1 Like

Absolutely. The way you’d simplify depends somewhat on your use-case. Are you making a timer or countdown? Or are you trying to display a sequence of messages?

In both cases you’d use what’s called a for loop, or “counting loop”. These are sections of code that repeat some number of times. Let’s start with a few simple examples before we start working on your code:

print("Starting...")
for count = 1, 10 do
	print(count)
end
print("Finished!")

When you run this, you should see this in the output:

Starting...
1
2
3
4
5
6
7
8
9
10
Finished!

In essence, the loop repeats print(count) 10 times, each time with a new value for count, and then continues with the rest of your script.

Counter

So for a timer, you could do something like this:

for count = 1, 10 do
	script.Parent.Text = "Script has been running for " .. count .. " seconds"
	wait(1)
end

Alternatively, for a countdown, you’d have to make the loop count backwards. In the following example, it starts at 10 and then counts down by 1 until it reaches 1:

for count = 10, 1, -1 do
	script.Parent.Text = "Game will end in " .. count .. " seconds"
	wait(1)
end

The third number (-1) is called the “increment” and is the amount that count will increase every time the loop runs. Because it’s negative, count will decrease by 1 every time.

Series of messages

To display a series of messages, you’ll have to use a table. Tables are basically lists of information (they’re actually more than that, but we won’t bother with that right now), so we can use them to store your messages ahead of time.

We can also get the amount of items in a list by doing #messages - this will be useful here.

local messages = {
	"Hi, this is the first message",
	"And this is the second message",
	"And a third...",
	"And so on."
}

--Repeat once for every message we have
for count = 1, #messages do
	local message = messages[count] --Get the message from our table
	script.Parent.Text = message
	wait(2)
end

Hopefully this helps. Let me know if you have any questions!

2 Likes

The key to writing tidier code is not knowing only one concept (OOP). It’s knowing as many things about the language as you can, The more knowledge you have about the language, the more tools you have, the more efficient and tidy your code is.

In the example you’ve supplied, no OOP or metatables are required, learn those only when you’re advanced.

for i = 0, 10 do
wait(2)
script.Parent.Text = "Hello"..i
end

This uses ‘string manipulation’ and ‘for loops’ to make your code tidier.

Just keep learning lua, and eventually you will be able to piece things yourself with the knowledge you gain.

I appreciate all the ideas.
I will always learn and improve myself!

1 Like