How can I restart an for loop

Hello,

The title says all.

And I want this line to be getting restarted;

for i = 1, 6 do
-- More code

So when it reaches ‘6’ that line must be restarted again so it starts with ‘1’ again.

Every suggestion to solve this would be appreciated!!

have it under a while loop

while true do
for i=1, 6 do

To restart it, just make another. What you can do is just create a function and call that function every time you want it to run.

   Function loop()
                 for I = 1,6 do
                      — code
    End)

        Loop() — runs for loop

Lol the spacing got messed up but you get the idea. You can also set parameters if you want to pass into the function.

I tried that but nothing happens what it should do.

Show me the code you used in a reply.

while wait() do --repeats after looping is done
    for i = 1, 6 do
        --code
    end
end

I think it doesn’t works because that function you showed me in a reply is also placed in another function and I think that that is blocking it.(i’m not sure…)


local Players = game:GetService("Players")

local function onPlayerAdded(player)
	print(player.Name)
	local names = {"Monica", "Joey", "Sarah", "Phoebe", "Michel", "Michael", "James", "Adam", "ElectroShop", "Cablebar" ,"Airedable" ,"Tv.Inc", "Videosify", "Tellyware", "Onaired", "Remixus", "Technomax", "Elevatelly", "Envideos", "Inc.ia", "Cotv", "Coremix", "Edaired", "Unitelly"}
	wait(5)

	local ReplicatedStorage = game.ReplicatedStorage.RemoteFunctions.AbortFunctions.Order1.Order1Function
	local ReplicatedStorage1 = game.ReplicatedStorage

	local tvOrdersRMD = {"OldTv", "Salora58UA330"}

	local orders = player.PlayerGui.ComputerMenu.Orders.Background1.Orders
	local OrderParent = player.PlayerGui.ComputerMenu.Orders.Background1

	-- The 1st parameter defaults to 1 if it's not specified, so in this case we can just put #companyNAMES to find the max number in the table.
	local randomtask2 = math.random(#names)

	local index
	local index2
	wait(3)
	
	local function loop()
	for i = 1, 6 do
			index = math.random(#names)
			index2 = math.random(#tvOrdersRMD)

			local Order = OrderParent["Order"..i]

			Order.Visible = true
			Order.Text = "Name: ".. names[index]
			Order.TVSelecter.Text = "Order: "..tvOrdersRMD[index2]
			orders.Value += 1



			local OrderValue = Instance.new("IntValue")
			OrderValue.Name = tvOrdersRMD[index2]
			OrderValue.Parent = ReplicatedStorage1:WaitForChild("Folder"..i)
			if OrderValue.Name == "OldTv" then
				OrderValue.Value = 15
			else
				OrderValue.Value = 120
			end

			print(i)
			if i == 6 then
				script.Disabled = true
				wait(20)
				script.Disabled = false
			end
		end

		loop()
	
		
		
		
		
		wait(5)
	end
end







Players.PlayerAdded:Connect(onPlayerAdded)

I think you need to call it outside of the function, see if that helps. What that does is just infinitely loop it.

1 Like

That’s because you’re disabling the script, therefore, you’re completely breaking it. Instead, leave wait(20) as the only line, as it’ll pause the code itself for 20 seconds.

1 Like

Ah I just realized I forgot another end, the loop() call was supposed to be outside of the function.

1 Like

Yea I know I don’t know why I putted script.Disabled there but anyways I solved the problem thanks to @Doqee and @ComplicatedParadigm.

I tested both of your replies and both of them works so yea! Thanks a lot :ok_hand:

Unfortiunately I can’t mark both of you so i’ll just mark one of u. BUT that doesn’t means that the other one didn’t helped at all!

1 Like