0
votes

I have the following function which checks if the given parameter is found as a key in a key-value table. If that is the case it should return true and break out of the loop. If nothing is found then do nothing.

function checkId(id)
  for k,v in pairs(info) do
    if id == tostring(k) then
      return true
      break -- break out of loop. mission accomplished.
    end
  end
end

I get an

'end' expected (to close 'do' at line 192) near 'break'

when I try to run this script. What am I missing?

1
You can't return and break. return exits the function. In lua 5.1 return also has to be the last statement in a block. - Etan Reisner
What do you suggest to do in this case? I need this function to return something if the condition is fulfilled. - sceiler
ha how stupid of me. thank you, it works perfectly now. - sceiler

1 Answers

4
votes

Logically you can't return and break like that.

return exits the function immediately (so you don't need the break).

That specific error is because in lua return has to be the last statement in a block.