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

Checking if a certain number is around a number?

For example how would you check if a nine is close to ten but eight is too far from ten to count. 11 is also close to nine but 12 isn't. Can someone tell me a formula that is able to find if a number is about a different number?

1 comment

0

Can you explain your question more?

1 answer

I'm assuming you are giving a function a value (the number), and it is giving you back the two numbers closest to it?

If that is your case, here is a simple script

function getClosestNumbers(num)
	local num1 = num - 1
	local num2 = num + 1
	return num1, num2
end

print(getClosestNumbers(11)) -- Returns 10 & 12

But if you are trying to find whether a number is close to the first number or not, you just make a few modifications

function getClosestNumbers(num, numChecking, range)
	for i=0, range, 1 do
		local num1 = num - range
		local num2 = num + range
		if num1 == numChecking then
			return true
		else
			if num2 == numChecking then
				return true
			else
				return false
			end
			return false
		end	
	end
end

print(getClosestNumbers(11, 8, 1)) -- 11 is the number you have, 8 is what you want to check, and 1 is the range of numbers you will be checking through

Using this second script, if you leave the 1 as it is, the function returns false, but if you change it to 3, it returns true because 11-3 = 8.

I hope this helped!

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