Can I use numberrange on a number?

I am trying to see if a number is within the range of two other numbers, such as 80 and 100.

Instead of testing it using <>= and confusing myself, I’m wondering if there is a simpler way to do this, maybe a way where I can just check if it’s between two numbers using the NumberRange data.

1 Like

In Lua (the programming language used in Roblox), there isn’t a built-in NumberRange data type or similar that you can use to directly check if a number is within a specific range. However, you can create a simple function that checks if a number is between two other numbers. This could simplify your code and potentially reduce confusion. Here’s an example:

function isNumberInRange(n, low, high)
    return n >= low and n <= high
end

-- usage:
if isNumberInRange(90, 80, 100) then
    print("The number is in range!")
else
    print("The number is not in range.")
end

This function takes three arguments: n (the number you want to check), low (the lower limit of the range), and high (the upper limit of the range). The function returns true if n is greater than or equal to low and less than or equal to high, and false otherwise.

It’s important to note that this function includes the boundaries (low and high) in the range. If you want to exclude them, you can change the >= and <= operators to > and <, respectively.

2 Likes

Never thought about this. Great idea!

1 Like

Add onto this as @Tellygum want to see if they can use NumberRange, you can use it if it helps you visualize better, though I think it’s best to format the way @sylwek1100 had theirs working. You can use the following below:

local range = NumberRange.new(0, 5) -- (min, max)
local bool = range.Min > range.Max -- stores false

More info: NumberRange | Roblox Creator Documentation

I just read the first line about NumberRange not existing, but above shows otherwise, but but it doesn’t compare if it’s in range as you mentioned :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.