Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I shorten this script into less lines?

Asked by
130363 67
7 years ago
Edited 7 years ago

function onClick() script.Parent.Text = "Clean Workspace" wait(1) script.Parent.Text = "Cleaning Workspace." wait(1) script.Parent.Text = "Cleaning Workspace.." wait(1) script.Parent.Text = "Cleaning Workspace..." wait(1) script.Parent.Text = "Done Cleaning Workspace(50)" wait(1) script.Parent.Text = "Done Cleaning Workspace(49)" wait(1) script.Parent.Text = "Done Cleaning Workspace(48)" wait(1) script.Parent.Text = "Done Cleaning Workspace(47)" wait(1) script.Parent.Text = "Done Cleaning Workspace(46)" wait(1) script.Parent.Text = "Done Cleaning Workspace(45)" wait(1) script.Parent.Text = "Done Cleaning Workspace(44)" wait(1) script.Parent.Text = "Done Cleaning Workspace(43)" wait(1) script.Parent.Text = "Done Cleaning Workspace(42)" wait(1) script.Parent.Text = "Done Cleaning Workspace(41)" wait(1) script.Parent.Text = "Done Cleaning Workspace(40)" wait(1) script.Parent.Text = "Done Cleaning Workspace(39)" wait(1) script.Parent.Text = "Done Cleaning Workspace(38)" wait(1) script.Parent.Text = "Done Cleaning Workspace(37)" wait(1) script.Parent.Text = "Done Cleaning Workspace(36)" wait(1) script.Parent.Text = "Done Cleaning Workspace(35)" wait(1) script.Parent.Text = "Done Cleaning Workspace(34)" wait(1) script.Parent.Text = "Done Cleaning Workspace(33)" wait(1) script.Parent.Text = "Done Cleaning Workspace(32)" wait(1) script.Parent.Text = "Done Cleaning Workspace(31)" wait(1) script.Parent.Text = "Done Cleaning Workspace(30)" wait(1) script.Parent.Text = "Done Cleaning Workspace(29)" wait(1) script.Parent.Text = "Done Cleaning Workspace(28)" wait(1) script.Parent.Text = "Done Cleaning Workspace(27)" wait(1) script.Parent.Text = "Done Cleaning Workspace(26)" wait(1) script.Parent.Text = "Done Cleaning Workspace(25)" wait(1) script.Parent.Text = "Done Cleaning Workspace(24)" wait(1) script.Parent.Text = "Done Cleaning Workspace(23)" wait(1) script.Parent.Text = "Done Cleaning Workspace(22)" wait(1) script.Parent.Text = "Done Cleaning Workspace(21)" wait(1) script.Parent.Text = "Done Cleaning Workspace(20)" wait(1) script.Parent.Text = "Done Cleaning Workspace(19)" wait(1) script.Parent.Text = "Done Cleaning Workspace(18)" wait(1) script.Parent.Text = "Done Cleaning Workspace(17)" wait(1) script.Parent.Text = "Done Cleaning Workspace(16)" wait(1) script.Parent.Text = "Done Cleaning Workspace(15)" wait(1) script.Parent.Text = "Done Cleaning Workspace(14)" wait(1) script.Parent.Text = "Done Cleaning Workspace(13)" wait(1) script.Parent.Text = "Done Cleaning Workspace(12)" wait(1) script.Parent.Text = "Done Cleaning Workspace(11)" wait(1) script.Parent.Text = "Done Cleaning Workspace(10)" wait(1) script.Parent.Text = "Done Cleaning Workspace(9)" wait(1) script.Parent.Text = "Done Cleaning Workspace(8)" wait(1) script.Parent.Text = "Done Cleaning Workspace(7)" wait(1) script.Parent.Text = "Done Cleaning Workspace(6)" wait(1) script.Parent.Text = "Done Cleaning Workspace(5)" wait(1) script.Parent.Text = "Done Cleaning Workspace(4)" wait(1) script.Parent.Text = "Done Cleaning Workspace(3)" wait(1) script.Parent.Text = "Done Cleaning Workspace(2)" wait(1) script.Parent.Text = "Done Cleaning Workspace(1)" wait(1) script.Parent.Text = "Done Cleaning Workspace" wait(1) script.Parent.Text = "Clean Workspace" wait(1) end script.Parent.MouseButton1Click:connect(onClick)

How do I make this shorter and how to i make it so that they have to wait 56 seconds before they can click it again or can click it after the text Clean Workspace shows again? I am very new to lua coding. THX

0
On top of ScriptGuider's answer you could also have used variables so you wouldn't have to type out script.Parent so mmuch arrowman888 69 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

Advice

Important bit of coding advice; if there's ever a situation where you can achieve the same end-result without repeating the same line(s) of code, do it. Keep your code flexible, and clean. Some people find the phrase clean code very broad, and not very helpful in solving their "real problem", but it's quite the contrary. However, I won't personally be explaining the importance of clean code here, so for that I recommend you look at this article.

Loops

To get back on topic, a nice simple solution to the lengthy code you provided would be utilizing loops. A loop is a process handled by the computer for the programmer, which will execute or do a certain set of instructions forever, or until a condition is met. Different computer languages may implement loops differently, but they're all fundamentally the same. You'll generally hear about for and while loops, which (fundamentally speaking), are the only ones that really exist. Any other "kind" of loop, just adopts the same concept with different implementation (the repeat loop in Lua for example, which I've marked the differences between in this answer).


Point is, you can use a loop instead of repeating the same instruction on each line. I'll leave more in-depth explanations of how loops work and the differences between them, to your personal research or question asking. But to keep things short and simple, this would be a quick revision of your code:

-- I also recommend using variables whenever necessary (and they're necessary very often!)
local parent = script.Parent

-- Use "Connect" instead of "connect", the old method has since been deprecated.
parent.MouseButton1Click:Connect(function()
    parent.Text = "Clean Workspace"
    -- Using a numeric for loop to add a period to the end of the string each iteration
    for i = 1, 3 do
        wait(1)
        parent.Text = parent.Text .. "." -- This process is called concatenation - the act of "adding" one string value to another.
    end

    -- Using the same numeric for loop, with it's optional step variant that determins the increment and direction of the for loop.
    for i = 50, 1, -1 do
        wait(1)
        -- Same concatenation process, but here we can use our control variable to display the loop's increment value
        parent.Text = "Done Cleaning Workspace (".. i ..")"
    end

    wait(1); parent.Text = "Done Cleaning Workspace"
    wait(1); parent.Text = "Clean Workspace"
end)

If you have any questions, feel free to ask. Hope this helped.

0
Thanks for your help ScriptGuider but I don't get the part where is says parent.Text = "Done Cleaning Workspace (".. i ..")" 130363 67 — 7y
0
and I don't get another part why did you put semicolon at the end of wait(1) on line 20 and 21 130363 67 — 7y
0
Because if you want to run two pieces of code on the same line, you have to separate them with a semi colon. plasma_node 343 — 7y
0
Shouldn't waste your time spoonfeeding someone who didn't bother to look at any coding tutorial whatsoever and probably will quit soon. cabbler 1942 — 7y
View all comments (2 more)
0
I am very new to lua coding. 130363 67 — 7y
0
The semicolon is just to highlight the separation between the two instructions on both lines. It doesn't provide any functionality, it just goes back to what I was saying about clean code earlier. The "i" between the two parenthesis, is called concatenation. You should look it up, or ask a question about it on the site. ScriptGuider 5640 — 7y
Ad

Answer this question