(This is a portion of the same script that my last 3 unanswered questions used, but it isn't a duplicate question in case similarities are made in the code)
As of now, I have an if statement which checks if an input number to a function is within +- 8 of a table value. Everything works, except it isn't comparing right with my current equation (or any combination therein).
Current code snippet for those who like visuals --
mdist = 16; -- actually references an int value, but let's just call it 16. local Coordsx = { -- table is filled whenever something is okay-ed to spawn after this check. } function xcheck(xrand) -- xrand is a random number input within set parameters. local ixgood = 0; ixgood = 0; for index, value in pairs(Coordsx) do if (value+mdist < xrand and value-mdist < xrand) then -- This is the problem comparison. Obviously this doesn't logically work out, the signs are just placeholders. ixgood = ixgood + 1; wait(); else ixgood = ixgood - 1; wait(); end end if ixgood == #Coordsx then wait(); return "true"; -- Yes, it is a string on purpose. else return "false"; -- Yet again, on purpose. end end
I've tried having an (and) or (and) setup, and have mixed and matched so many sign combos (after logic eluded me) to no avail. At this point, if I could just have something to check if it's within +1, +2, +3, et cetera, (without hard typing it), it'd make things so much easier.
Thanks for reading, and I look forward to any offered help!
Use absolute values!
VALUE = 23 EXPECTED = 17 VARIANCE = 8 if math.abs(VALUE - EXPECTED) <= VARIANCE then print(string.format("%d is within %d of %d", VALUE, VARIANCE, EXPECTED)) end