Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Check if something is not between two numbers?

Asked by 7 years ago

I just have an if statement and I need it to check if a certain Value is not between the numbers 60 to 80. Here is the block of code I need help with it's simple, just how do I make it check between these two numbers?

if h.Health ~= 60,80 then
    pg.Hurt.Red.ImageTransparency = 0.8 
end

I end up with "expected 'then' got ','" Any help is appreciated :)

0
Almost tried doign it how math.random does it with the whole (1,5) type deal but apparently thats not how this is done. CrispyBrix 113 — 7y

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago
Edited 7 years ago

A number is "between" 60 and 80 if it is greater than 60: 60 <= x and it is less than 80: x <= 80.

Thus,

if 60 <= x and x <= 80 then
    -- between
else
    -- not between!
end

You can use not to negate the whole thing:

if not (60 <= x and x <= 80) then -- parenthesis matter!
    -- not between!
else
    -- between
end

You can use De Morgan's law to get rid of the not. not (a and b) is (not a) or (not b). The not of a <= is > and the not of >= is < (flipping when the things are true):

if 60 > x or x > 80 then
    -- not between!
else
    -- between
end

"Flipping" the > to make them be in smallest to largest makes more sense:

if x < 60 or 80 < x then
    -- not between!

That is, x is not between 60 and 80 if it is less than 60, OR it is greater than 80.

Ad

Answer this question