Ad
Log in to vote
0

Is it possible to redo a 'for' script?

Asked by 4 years ago

I'm not asking for code of one example, but I am just wondering. Could you 'redo' or start over a for script?

for i = 1,10 do
print(i)
end

^ This is what I mean. Is it possible to restart it?

0
You could put it in a function so the for loop can run every time the function is called. A while loop can also work if you want it to restart periodically. Denny9876 1402 — 4y

2 answers

Log in to vote
9
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
4 years ago
Edited 4 years ago

Numerous ways to do the following. It specifically depends on how you would like tore-do your code. I will show you two most common and important ones.

Loops in general

functions

While True Do

while true do --The Infinite Loop
    for i = 1, 10, 1 do
         print(i)
    end 
    wait()
end 

if you come across in stopping the infinite loop, create an if statement and then break the while true do loop when the condition is met.

Example

local count = 0

while true do --The Infinite Loop
    if count >= 10 then break end 
    for i = 1, 10, 1 do
         print(i)
    end 
    count = count + 1
end 

The condition to break is when the count reaches the value of 10. Thus, the infinite loop breaks.

Warning, Always break a while true do loop. It can cause severe lag if done improperly

For Loop

You also could literally do the following

for number = 1, 10, 1 do
    for i = 1, 10, 1 do
         print(i)
    end 
end 

Yes, it is for loop inside for loop. Pretty straight forward so I won't go into much details.

Repeat...Until Loop

local count = 0

repeat 
    for i = 1, 10, 1 do
         print(i)
    end 
    count = count + 1

until count == 10

The name gives away the answer. Its function is to repeat the work inside. Who could have guessed that, aye?

Functions

Very important one actually. When you have a lot of stuff, blocks of code, you would want to switch over to functions for various reasons.

1) You are interacting with the game environment.

2) You are firing a remote event. and so on............

local count = 1

local function repeatCodeBelow ()
    for i = 1, 10, 1 do
         print(i)
    end 
end 

while true do 
    if count >= 10 then break end 
    repeatCodeBelow()
    count = count + 1 
end 

Of course, you aren't limited to the ones shown above. Experiment with it and you will eventually know which one is right to use at appropriate time.

Learn more [API]

Have a lovely day of coding
Ad
Log in to vote
-1
Answered by 4 years ago

If you just want to redo the loop do

local function forLoop() -- "local" is optional
    for i = 1,10 do
        print(i)
    end
end

-- elsewhere
forLoop()
-- yet again elsewhere
forLoop()

If you want to completely make the script execute again, move it in and out of the Workspace, like this

-- Some random script
-- we pretend the name of the script with the loop is ForLoopScript
local ForLoopScript = game.Workspace.ForLoopScript
ForLoopScript.Parent = game:GetService("Lighting") -- basically game.Lighting
-- anyways, that was a placeholder, and now ForLoopScript is in the Lighting service. You really can put it nearly anywhere as long as it isn't still a descendant of the Workspace
ForLoopScript.Parent = game.Workspace

Here we exploit the fact that scripts put in Workspace execute immediately. Since we moved it out and then returned it, Roblox makes the script execute again, repeating the loop.

If you want the loop to execute every once in a while:

-- A while loop
while true do
    for i = 1,10 do
        print(i)
    end
    wait() -- If this isn't here, you will not go to space today. wait() can be blank or have a number in it.
end
-- or a virtually identical method
while wait() do
    for i = 1,10 do
        print(i)
    end
end
-- A different way to do it
while true do
    for i = 1,10 do
        print(i)
    end
    game:GetService("RunService").RenderStepped:Wait() -- This will cause the loop to run every single rendering frame.
end

Answer this question