For loop struggles

local Size = 10
local Resolution = 2

for Count1 = 1, Size - Resolution, Resolution do
	if Count1 == Size - Resolution then
		warn("Last!")
	end
end

The code looks simple but the trouble I am having is with the resolution. It will not always print out “Last” depending on what resolution it is.

For example with a resolution of 1 it will print out last but if I change the resolution to 2 it will not.

How can I fix this so it always prints out last iteration, regardless of the resolution?

That’s because you’re iterating an odd-number of values with an even-number of steps. Your example shows 1 to 8, step interval 2. That’s 1, 3, 5, 7. You don’t reach 8.

“How can I fix this so it always prints out last iteration, regardless of the resolution?”

By addressing that it’s even-steps in an odd-interval.
Alternatively:

if Count1 + Resolution > Size - Resolution then
	warn("Last!")
end

as 7+2 > 8 it must be the last interval.

1 Like