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

How can I shorten a long and repetitive block of code?

Asked by 3 years ago
Edited 3 years ago

I just copy and pasted my timer. I was hoping there would be a loop or something that I don't know about. My code would go something like this:

function ValueCountDown ()
NumberValue.Value = 7
NumberValue.Value = NumberValue.Value - 1
NumberValue.Value = NumberValue.Value - 1
NumberValue.Value = NumberValue.Value - 1
NumberValue.Value = NumberValue.Value - 1
NumberValue.Value = NumberValue.Value - 1
NumberValue.Value = NumberValue.Value - 1
NumberValue.Value = NumberValue.Value - 1

   end)

This is not what I actually use in my script (It actually took up 99 lines of the same line). I'm aware that you can press the arrow on the side to shorten you're function but can someone tell me a loop I can use as well as an example of it.

2 answers

Log in to vote
1
Answered by
CodeWon 181
3 years ago

A For-Loop would be useful

for count = 1,10,1 do
    -- Code
    count - count-1
end

The count is just a variable, you name it what ever you want. As a video I watched said-S.E.I. The first 1 is where the number starts, the 10 is where it ends. The last 1 is the increase, the timer will increase by 1. To make it count down backwards you could something like:

for count = 7,0,-1 do
    -- Code
    count = count-1
end

If its to fast, just add a wait in the count.

Hope this helps!

0
I don't think you really know how for loops work. Changing count inside the loop is unnecessary and I actually don't know what happens when you do Spjureeedd 385 — 3y
Ad
Log in to vote
2
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

You can use repeat loop, but I'd recommend you for loops because you don't need to make a variable. Well it is optional, all depends on you.

for i = NumberValue.Value, 0, -1 do -- First value means for the start, second value means for the end, and the third variable is how much number you want to skip from start to end. Like from 99 to 0, you want to skip -1 value between.
    NumberValue.Value = i -- Set the value to the index
    wait()
end
0
Here you could just do -- NumberValue.Value = i -- inside the loop Spjureeedd 385 — 3y
0
Good idea! I forgot about that thank you! Xapelize 2658 — 3y

Answer this question