Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would I write an if then statement to track when the for i do loops i changed to a number?

Hi! :)

My question is, I want to have an if then statement in my for i do loop and the if then statement would catch when the i value got to a certain value, then it would break the for loop.

Here's my attempted code:

for i = 0, 1, 0 do
	--I have code in here written that isn't related to my question, so it's not needed
	if i == 0, 1, 1 then
		break
	end
end

I know you cant do on Line 3 "if i == 0, 1, 1 then" because in the if then statement it's supposed to be one value, not multiple values and not have the commas ( "," ) in it.

I also tried this, which didn't work:

for i = 0, 1, 0 do
	--I have code in here written that isn't related to my question, so it's not needed
	if i == 1 then
		break
	end
end

That code, I know, was kinda stupid in a way because what I wanted the if then statement to be triggered on when the third value (That value in the for loop was 0) was 1. And I always knew that code doesn't make things easy for you. :/ So can someone please try to figure out my question?

Much appreciated! ~Chez_Guy :)

4 comments

0

The break in your if statement is only breaking out of the current if statement which is why,

0

@LegitmateTrades thats what i thought.. but are you sure?

0

Yes, I've had this same issue before.

0

No, break breaks the loop

1 answer

You are doing it correctly besides for the break part. The break statement is only breaking out of the current if statement so in this case the

local forNumberLoop = function(number)
	for i = 0, 1, 0 do
		if i == number then
			return
		end
	end
end)

forNumberLoop(1)

part. Put it in a function and then return from that function and it will break from the original for loop.

4 comments

0

but would the code tell which value would be 1 and which one i was looking for when it's 1?

0

Interchange the value you want with 1 so for example make a var called Number and set it to the number you are looking for then do "if i == number then"

0

Can you edit the answer what you think i should do? I'm stumped.

0

Edited

This discussion is preserved as it was. New answers, comments, and votes are closed.