help to stop a boucle and obtain only a value

hi, i can’t stop my boucle 

the goal is find the nearest character from my event.x from a touch function and assign to him a function.

the snippet :

--cnt.comment=0 --#character=30 local function commentCharacter(event,character)            for i=1,#character do               print(character[i].x,character[i].y)               if character[i].x +150 \> event.x and character[i].x -150 then                    cnt.comment=cnt.comment+1                    if cnt.comment == 1 then cnt.comment=0                        print(i,"i")                    end               end     end end commentCharacter(event,character) --result : i,1 i,4 i,8

or what i would like is only a value

--result: i,1 

thanks for your help.

Hi.

A couple of things.

This

and character[i].x -150 

appears to be incomplete.

Instead of

 cnt.comment=cnt.comment+1 if cnt.comment == 1 then cnt.comment=0 print(i,"i") end

maybe you want

print(i, "i") return -- in this case, break would also work

hi StarCrunch,

thanks for your response. 

for your first comment, it’s a typo by myself  :rolleyes: i have simplified my code and i forgot a part. 

With break, effectively it works.

Have a good day.

Hi.

A couple of things.

This

and character[i].x -150 

appears to be incomplete.

Instead of

 cnt.comment=cnt.comment+1 if cnt.comment == 1 then cnt.comment=0 print(i,"i") end

maybe you want

print(i, "i") return -- in this case, break would also work

hi StarCrunch,

thanks for your response. 

for your first comment, it’s a typo by myself  :rolleyes: i have simplified my code and i forgot a part. 

With break, effectively it works.

Have a good day.