A function I made that uses a for loop but it is more customizable

function CustomForLoop(Size, Skip, Repeat, Reverse, Function)
	Size = type(Size) == "number" and {1, Size} or Size
	Skip = type(Skip) == "number" and {Skip} or Skip
	for i = 1, Size[2] * Repeat, 1 do
		local Count = i % Size[2] ~= 0 and i % Size[2] or Size[2]
		local ReverseCount = Reverse == true and Size[2] - Count + 1 or Count
		local Continue = false
		for ii = 1, #Skip, 1 do
			if Reverse then
				if ReverseCount % Skip[ii] == 0 then
					Continue = true
				end
			else
				if Count % Skip[ii] == 0 then
					Continue = true
				end
			end	
		end
		if Continue == true or ReverseCount < Size[1] then
			continue
		end
		Function(ReverseCount, math.ceil(i / Size[2]))
	end	
end

CustomForLoop(5, 2, 3, true, function(Count, Repeat)
	print(Count, Repeat)
end)

ok so lets get started
the function CustomForLoop has 5 parameters

the first one is the Size of the for loop, this can be a single integer or a table of two integers
basically Size controls the Start and Finish of the for loop

the second one is the Skip of the for loop, this can be a single integer or a table of multiple integers
basically Skip will not run for every Count that is divisible by that number

the third one is the Repeat of the for loop, this can be a single integer
basically Repeat will run it multiple times depending on its amount

the fourth one is the Reverse of the for loop, this can be a single integer
basically Reverse will reverse the for loop

the fifth one is the function that handles what happens, this is a function obviously
as you can see I am printing out Count and Repeat in my function

the code above prints this
image
if you have ANY questions please ask me
also I don’t think this is useful enough to be in #resources:community-resources

EDIT: I just wanted to say that if Skip was equal to 1 or Repeat was equal to 0 then the function would never run
the Repeat one is obvious, but the Skip when set to 1 skips every number divisible by 1 so obviously it never runs the code

2 Likes