Why is Luau giving me a warning here?

Why is Luau warning me here? I am guarding against the nil-case in line 7, so presumably it shouldn’t complain here at line 11. Is there a proper way of resolving this warning?

Bah, adding assert(character) at line 10 fixes it.

if not character then
Instead of:
if character == nil

That did not fix the issue when I tried that, fyi. I suppose I should have listed my attempted fixes in my original post.

Doesn’t seem to recognize the implications of having that first ‘return false’. Prob just expects the second if to be an elseif. Combining into one if statement with logical operators would prob work too, but I remember reading somewhere that it’s preferable to use the elseif approach because the other way can confuse the system in some circumstances, or something. Don’t quote me on that last bit. I don’t want to start rumors. :grinning:

Edit:
Yeah, sry. Checked thru the github repo and don’t see anywhere in the docs where this case is discussed, and my C++ is not good enough to find it in the src (searching for the warning wasn’t super helpful). Having someone with knowledge of what’s happening here would be great, but my suspicion is that the linting pass is seeing that your Model could be nil in the function scope and since both of those if statements are at the same level within that scope, it’s flagging the second without consideration of what’s happening inside the first. It will warn if Model is used in any scope inside the function where Model ~= nil isn’t an explicit condition of being in that scope. Is just a guess that using elseif is the proper way to resolve it. Nesting code that uses Model inside a Model ~= nil check in some fashion is prob the way to go. I guess assert works because it just aborts the fn once that’s triggered. Since that assert would never be triggered in your example, one would hope that isn’t the fix they had in mind when designing this.

Oh, here it is; can quote that.

Note: In Luau, the if-then-else expression is preferred vs the standard Lua idiom of writing a and b or c (which roughly simulates a ternary operator). However, the Lua idiom may return an unexpected result if b evaluates to false. The if-then-else expression will behave as expected in all situations.

1 Like

Was a shot in the dark, it’s the typecasting causing the issue, might be worth reporting.