Quantcast

Intro to Finite Loops

This post is meant to help you simplify/shorten your Event-String

Loops, loops, loops: not all maps may choose to use them. But when it comes to loops, it’s best to make it as simple for you to work with as possible. In this post, I will be explaining how you can simplify finite loops; a loop that repeats a certain amount of times.

Finite Loops

To help out, suppose I want a part in a map to flash 3 times once a button is pressed. Okay then, I would type out the code that would look something like this:

local Lib = workspace.Multiplayer.GetMapVals:Invoke() --Has: Map, Script, Button, btnFuncs
Lib.Button:connect(function(p, bNo) if Lib.btnFuncs[bNo] then Lib.btnFuncs[bNo](bNo, p) end end)

Lib.btnFuncs[1] = function()
    wait(.5)
    Lib.Map.Flash_Part.Transparency = 1
    wait(.5)
    Lib.Map.Flash_Part.Transparency = 0
    wait(.5)
    Lib.Map.Flash_Part.Transparency = 1
    wait(.5)
    Lib.Map.Flash_Part.Transparency = 0
    wait(.5)
    Lib.Map.Flash_Part.Transparency = 1
    wait(.5)
    Lib.Map.Flash_Part.Transparency = 0
    wait(.5)
    Lib.Map.Flash_Part.Transparency = 1
    Lib.Map.Flash_Part.CanCollide = false
end

For now, this would work for 3 flashes. But what if I wanted the part to flash 20 times? If I continued to copy and paste the same code over and over, the script would now become SUPER long, and it would be time consuming.

Instead, you can simplify the work by using a for i = s,e do statement.
(“s” is the starting number, and “e” is the ending number)

Since I wanted the part to flash 3 times, “s” would equal 1, and “e” would equal 3. And now, the for statement will now repeat the sequence 3 times:

local Lib = workspace.Multiplayer.GetMapVals:Invoke() --Has: Map, Script, Button, btnFuncs
Lib.Button:connect(function(p, bNo) if Lib.btnFuncs[bNo] then Lib.btnFuncs[bNo](bNo, p) end end)

Lib.btnFuncs[1] = function()
    for i = 1,3 do
        wait(.5)
        Lib.Map.Flash_Part.Transparency = 1
        wait(.5)
        Lib.Map.Flash_Part.Transparency = 0
    end
    Lib.Map.Flash_Part.Transparency = 1
    Lib.Map.Flash_Part.CanCollide = false
end

Notice how much smaller the code is? That’s the point of using for!

What is the point of “i”?
The variable “i” is there to represent the current number/interval. If the loop is on its 2nd repeat, “i” will equal 2.

Remember: for will start at the starting number (1), and repeat the function inside until “i” equals the ending number (3).

Take it a Step Further (Challenge)

Now that I have made the code easier to work with, what if I now decide to make the flashes faster each time? Easy! All I would have to do is replace the wait(.5) with “i”, right?

Well, if you want it to flash slower, then yes. But this time, we need “i” to become smaller each time. Therefore, we can just use 3-i*.25 inside wait().

When i = 1, wait() is now wait(3-.25) = 2.25
When i = 2, wait() is now wait (3-.5) = 1.5
And so on…

local Lib = workspace.Multiplayer.GetMapVals:Invoke() --Has: Map, Script, Button, btnFuncs
Lib.Button:connect(function(p, bNo) if Lib.btnFuncs[bNo] then Lib.btnFuncs[bNo](bNo, p) end end)

Lib.btnFuncs[1] = function()
    for i = 1,3 do
        wait(3-i*.25)
        Lib.Map.Flash_Part.Transparency = 1
        wait(3-i*.25)
        Lib.Map.Flash_Part.Transparency = 0
    end
    Lib.Map.Flash_Part.Transparency = 1
    Lib.Map.Flash_Part.CanCollide = false
end

And that’s it! I will [hopefully] be posting more of these kinds of tips in the future. Hopefully this post helped you out!

tbh it doesnt look good unless you use TweenService

Oki dwn

rip my brain

1 Like

hello there! do you know how to make so it changes brick colour? It would help me a lot. Thanks!

1 Like

while true do
Lib.Map.Part.Color = Color3.fromRGB(255,0,0)
wait(0.2)
Lib.Map.Part.Color = Color3.fromRGB(0,0,0)
end

1 Like

the 3-i*.25 thing killed my brain too

I wanted to see how scripting looks and omg what is all those stuff I AM OUT OF HERE