Does this work in an if statement?

This is an extremely simple question.

local n = math.random(1, 5)
if 0 < n < 3 then
		
	end

Does that work? Testing it in my case is a little hard, and while there’s no red squiggly, it just feels a little off to me.

I don’t think Lua supports if statements like that, I could be wrong though

Are you wanting to find the range if the n variable is greater than 0, and less than 3?

Yes, and I know I could just do a little

if n > 0 and n < 3 then

but this just seemed easier and more convenient, and i was hoping it’d work.

Unfortunately, the “easier way” just results as an error

I believe this error is cast due to how your Data Types are organized, and we’re attempting to compare a Boolean (Which would be the true/false values) to a Number,

Printing this:

local Num = math.random(1, 5)
print(Num, 0 < Num > 3)

Results as this:
image

But how the code looks at it, is this:

0 < Num == true or false
true or false > 3 == ERROR

This will not work! Statements such as 0<n return a bool, thus for the second part of the statement (x<3) x is equal to the bool that is returned from 0<n. Then you try to compare the bool to a number (e.g false < 3) and you get an error, something along the lines of “attempt to compare bool with number”.

Read more here:

Eh, Idk, try this tho and fix it to however you want if you need this.


while true do
wait(1)
local n = math.random(1,3)
print(n)
if n == 1 or n == 3 then
print(n)
end
end