Jump to content




Error in if check


  • You cannot reply to this topic
3 replies to this topic

#1 Bye.

  • Members
  • 59 posts

Posted 14 October 2015 - 02:24 PM

I have a problem with this if:

if not fs.getFreeSpace("/") >= 100 then

the return is:

startup:103: attempt to compare boolean with number

Can you please help me?
Thanks

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 14 October 2015 - 02:31 PM

The not happens before the comparison. You should remove the not and use < instead of >=.

#3 Bye.

  • Members
  • 59 posts

Posted 14 October 2015 - 02:57 PM

Thanks a lot but I don't fully understand why its happened

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 14 October 2015 - 05:26 PM

Order of operations. As you can see in the operator precedence list, the `not` operator has a higher precedence than `>=` does. So, first, the result of the fs.getFreeSpace call is Boolean NOT'd, which would turn any number to a Boolean false value. Then it tries to compare `false >= 100`, which is where the error is thrown. So instead, you should change the comparison operator to make the comparison you're actually interested in, rather than trying to use the opposite comparison and inverting the result (which would work if you used parentheses to force the comparison to happen before the inversion, but there is no good reason to do that).





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users