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?
Loops in general
functions
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.
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.
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.
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?
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.
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