• Quick question about the use of "or" and "and" when checking for something.
    4 replies, posted
Sorry about the really crappy title. Not sure how to word it very well. Anyway, just asking about something that's confused me for a while. I wanted to return a function that returns true when its one of the chosen teams. [code] return self:Team() == 1 or 2 [/code] Is that correct? Would that return true if the player was 1 or 2? Also, how would you go about checking for a lot of teams? If I had 10 teams I wanted to all return true for that function, how would I do that? Usually I would keep on repeating (or self:Team() == 3 or self:Team() == 4), but that is just really painful and horrendous coding.
[CODE] return self:Team() == 1 or 2 [/CODE] This doesn't work because Lua interprets self:Team() == 1 as true if they're on team 1 and false if they're on team 2, so you're really asking: [CODE] return false or 2 [/CODE] Depending on whether or not the player's team is 1. Really, what you need to do is this: [CODE] return self:Team() == 1 or self:Team() == 2 [/CODE]
Yeah, that's what I thought. Damnit though. Guess my code will still look bad then ;( Also just one other question, I see people doing stuff like [code]return 1 or 2 and 3 or 4[return] How does that work? (That might not have been the best example ever...)
Also, if you have a lot of teams, usually people use tables like so: [CODE] local tab = { [1] = true, [2] = true, [3] = true } return tab[self:Team()] ~= nil [/CODE] [editline]16th July 2016[/editline] sorry, automerge Well, if people return multiple things like that, usually they're just checking whichever one isn't nil (or whichever one is true), for example: [CODE] return nil or nil or 2 or nil [/CODE] That would return 2 Also, this returns true: [CODE] return false or false or false or true [/CODE] Also, for your code I think you could simply do [CODE] return ply:Team() >= 1 and ply:Team() <= 10 [/CODE] That'd return true if the player's team was between 1 and 10
[B]*facepalm*[/B] Yep, that would work fine. Thanks a lot!
Sorry, you need to Log In to post a reply to this thread.