Loop for was supposed to run 5 times, but runs only 1

Asked

Viewed 56 times

0

So I’m having a problem with a loop for in the code below:

for i=1,#collisionBlocks do
    t=collisionBlocks[i]
    print(#collisionBlocks)
    print(i)
    if player.y-1<t[4] and player.y-1>t[2] and ((player.x>=t[1] and player.x<=t[3]) or (player.x+50>=t[1] and player.x<=t[3])) then print'nao da para andar pra cima' return false end
    return true
end

The table collisionBlocks has 5 other tables inside, but the loop runs only once, being that it was to run 5 times since the table collisionBlocks has 5 items inside it. So much so that if you perform the function that runs this loop, it will appear:

>5
>1

you should show up:

>5
>1
>5
>2
>5
>3
>5
>4
>5
>5
  • 1

    What are the return inside the loop? Does that belong to a function? If so, the return will always finish the function, regardless of whether there are more iterations in the loop.

  • 1

    Ah, I know what’s causing it. The return true at the end of the loop was to be out of the loop right after it finished. Thank you very much!

  • The show’s coming off for when I find any return. You have to think of a way to do this without using the return.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.