Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Expression for "0 through X" with a +- if compare?

(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!

2 comments

0

What is the random input for?

0

It's for finding a suitable x coordinate to compare with the current x coordinates in Coordsx. Much of the script is omitted to make it seem less intimidating.

1 answer

✓ Accepted answer

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

1 comment

0

Wow, this worked extremely well. I implemented it for both the x and z functions and now it works great. (I did however change <= to >= since it did the opposite at first) Thank you for the help!

This discussion is preserved as it was. New answers, comments, and votes are closed.