Help condensing ModuleScript for getting the corners of a BasePart

Hi, i a have made a ModuleScript for getting the Corners of a BasePart, but i find my code too long. i tried with tables, but it was to hard. Need help!

If we are to help you, we would need to see the script.

3 Likes

Yes, it was too large. I am trying to link a Github link, pls wait!

1 Like

@IAmPinleon I added it:

Here’s a more efficient way to check for your corners and not have that hugeeeee block of code!

The code has annotations just to make sure you’re following exactly how it functions. One thing to note is that all the functions (as you have it currently set up) only check for less than or greater than comparisons. If you expect or want equal values to return true, you’ll have to just modify the functions to do that (obviously no big deal, just something to note).

Code
--Basic function to compare values, returns true if Input1 is greater than Input2
local function CompareGreaterThan(Input1, Input2)
	if Input1 > Input2 then
		return true
	else
		return false
	end
end

--Basic function to compare values, returns true if Input1 is less than Input2
local function CompareLessThan(Input1, Input2)
	if Input1 < Input2 then
		return true
	else
		return false
	end
end

--If you're constantly comparing the same sets of values (which I think you are), you could try this
--I put in a couple of your values to show how this would be set up
local ComparisonTable = {
	{Value1 = TopUnderRightCorner.X, Value2 = TopUnderLeftCorner1.X, Function = CompareLessThan};
	{Value1 = TopUnderRightCorner.X, Value2 = TopUnderRightCorner1.X, Function = CompareGreaterThan}
}

--This replaces that huge chunk of code that checks all those values
local function CheckValues()
	local TotalTrue = true --Initially set this to true (assuming everything runs well, this will stay true)
	for i, v in pairs (ComparisonTable) do --Parses through the ComparisonTable
		if v.Function(v.Value1, v.Value2) == false then --At each element in the table (v) we plug Value1 and Value2 into the function specified (v.Function).
			TotalTrue = false --If it returns false, we set TotalTrue to false, because all that's needed for the check to return false is for one condition to not be met
		end
	end
	return TotalTrue --Return the value of TotalTrue.  If everything checked out, this will be true.  If any of the conditions failed, this will be false
end

--Apply like this
if CheckValues() == true then --CheckValues() will be replaced by the returned value of either true or false
	print("Conditions met!")
	--Code that is executed if true!
else
	print("One of the compared conditions was returned as false!")
end

Here’s an example of place of it in action too if that will help.

EfficientCheck.rbxl (18.9 KB)

3 Likes

@https_KingPie, good, but i compare all single Vertex with all single Vertex of a Region3. Then my code is very precise and i will evite all sort of bug.

This shouldn’t cause any sort of bug, it’s just a simplification of

this check
--Control all the corners, ALL!
		if TopUnderRightCorner.X < TopUnderLeftCorner1.X and TopUnderRightCorner.X > TopUnderRightCorner1.X and TopUnderRightCorner.Y < TopUnderRightCorner1.Y and TopUnderRightCorner.Y > BottomUpperRightCorner1.Y and TopUnderRightCorner.Z < TopUpperRightCorner1.Z and TopUnderRightCorner.Z > TopUnderRightCorner1.Z 
		and TopUnderRightCorner.X < BottomUpperLeftCorner1.X and TopUnderRightCorner.X > BottomUpperRightCorner1.X and TopUnderRightCorner.Y < TopUpperRightCorner1.Y and TopUnderRightCorner.Y > BottomUnderRightCorner1.Y and TopUnderRightCorner.Z < BottomUnderRightCorner1.Z and TopUnderRightCorner.Z > BottomUpperRightCorner1.Z 
		and TopUnderRightCorner.X < TopUpperLeftCorner1.X and TopUnderRightCorner.X > TopUpperRightCorner1.X and TopUnderRightCorner.Y < TopUpperLeftCorner1.Y and TopUnderRightCorner.Y > BottomUnderLeftCorner1.Y and TopUnderRightCorner.Z < TopUpperLeftCorner1.Z and TopUnderRightCorner.Z > TopUnderLeftCorner1.Z 
		and TopUnderRightCorner.X < BottomUnderLeftCorner1.X and TopUnderRightCorner.X > BottomUnderRightCorner1.X and TopUnderRightCorner.Y < TopUnderLeftCorner1.Y and TopUnderRightCorner.Y > BottomUpperLeftCorner1.Y and TopUnderRightCorner.Z < BottomUnderLeftCorner1.Z and TopUnderRightCorner.Z > BottomUpperLeftCorner1.Z 
		and TopUpperRightCorner.X < TopUnderLeftCorner1.X and TopUpperRightCorner.X > TopUnderRightCorner1.X and TopUpperRightCorner.Y < TopUnderRightCorner1.Y and TopUpperRightCorner.Y > BottomUpperRightCorner1.Y and TopUpperRightCorner.Z < TopUpperRightCorner1.Z and TopUpperRightCorner.Z > TopUnderRightCorner1.Z 
		and TopUpperRightCorner.X < BottomUpperLeftCorner1.X and TopUpperRightCorner.X > BottomUpperRightCorner1.X and TopUpperRightCorner.Y < TopUpperRightCorner1.Y and TopUpperRightCorner.Y > BottomUnderRightCorner1.Y and TopUpperRightCorner.Z < BottomUnderRightCorner1.Z and TopUpperRightCorner.Z > BottomUpperRightCorner1.Z 
		and TopUpperRightCorner.X < TopUpperLeftCorner1.X and TopUpperRightCorner.X > TopUpperRightCorner1.X and TopUpperRightCorner.Y < TopUpperLeftCorner1.Y and TopUpperRightCorner.Y > BottomUnderLeftCorner1.Y and TopUpperRightCorner.Z < TopUpperLeftCorner1.Z and TopUpperRightCorner.Z > TopUnderLeftCorner1.Z 
		and TopUpperRightCorner.X < BottomUnderLeftCorner1.X and TopUpperRightCorner.X > BottomUnderRightCorner1.X and TopUpperRightCorner.Y < TopUnderLeftCorner1.Y and TopUpperRightCorner.Y > BottomUpperLeftCorner1.Y and TopUpperRightCorner.Z < BottomUnderLeftCorner1.Z and TopUpperRightCorner.Z > BottomUpperLeftCorner1.Z
		and BottomUpperRightCorner.X < TopUnderLeftCorner1.X and BottomUpperRightCorner.X > TopUnderRightCorner1.X and BottomUpperRightCorner.Y < TopUnderRightCorner1.Y and BottomUpperRightCorner.Y > BottomUpperRightCorner1.Y and BottomUpperRightCorner.Z < TopUpperRightCorner1.Z and BottomUpperRightCorner.Z > TopUnderRightCorner1.Z
		and BottomUpperRightCorner.X < BottomUpperLeftCorner1.X and BottomUpperRightCorner.X > BottomUpperRightCorner1.X and BottomUpperRightCorner.Y < TopUpperRightCorner1.Y and BottomUpperRightCorner.Y > BottomUnderRightCorner1.Y and BottomUpperRightCorner.Z < BottomUnderRightCorner1.Z and BottomUpperRightCorner.Z > BottomUpperRightCorner1.Z
		and BottomUpperRightCorner.X < TopUpperLeftCorner1.X and BottomUpperRightCorner.X > TopUpperRightCorner1.X and BottomUpperRightCorner.Y < TopUpperLeftCorner1.Y and BottomUpperRightCorner.Y > BottomUnderLeftCorner1.Y and BottomUpperRightCorner.Z < TopUpperLeftCorner1.Z and BottomUpperRightCorner.Z > TopUnderLeftCorner1.Z
		and BottomUpperRightCorner.X < BottomUnderLeftCorner1.X and BottomUpperRightCorner.X > BottomUnderRightCorner1.X and BottomUpperRightCorner.Y < TopUnderLeftCorner1.Y and BottomUpperRightCorner.Y > BottomUpperLeftCorner1.Y and BottomUpperRightCorner.Z < BottomUnderLeftCorner1.Z and BottomUpperRightCorner.Z > BottomUpperLeftCorner1.Z
		and BottomUnderRightCorner.X < TopUnderLeftCorner1.X and BottomUnderRightCorner.X > TopUnderRightCorner1.X and BottomUnderRightCorner.Y < TopUnderRightCorner1.Y and BottomUnderRightCorner.Y > BottomUpperRightCorner1.Y and BottomUnderRightCorner.Z < TopUpperRightCorner1.Z and BottomUnderRightCorner.Z > TopUnderRightCorner1.Z
		and BottomUnderRightCorner.X < BottomUpperLeftCorner1.X and BottomUnderRightCorner.X > BottomUpperRightCorner1.X and BottomUnderRightCorner.Y < TopUpperRightCorner1.Y and BottomUnderRightCorner.Y > BottomUnderRightCorner1.Y and BottomUnderRightCorner.Z < BottomUnderRightCorner1.Z and BottomUnderRightCorner.Z > BottomUpperRightCorner1.Z
		and BottomUnderRightCorner.X < TopUpperLeftCorner1.X and BottomUnderRightCorner.X > TopUpperRightCorner1.X and BottomUnderRightCorner.Y < TopUpperLeftCorner1.Y and BottomUnderRightCorner.Y > BottomUnderLeftCorner1.Y and BottomUnderRightCorner.Z < TopUpperLeftCorner1.Z and BottomUnderRightCorner.Z > TopUnderLeftCorner1.Z
		and BottomUnderRightCorner.X < BottomUnderLeftCorner1.X and BottomUnderRightCorner.X > BottomUnderRightCorner1.X and BottomUnderRightCorner.Y < TopUnderLeftCorner1.Y and BottomUnderRightCorner.Y > BottomUpperLeftCorner1.Y and BottomUnderRightCorner.Z < BottomUnderLeftCorner1.Z and BottomUnderRightCorner.Z > BottomUpperLeftCorner1.Z
		and TopUnderLeftCorner.X < TopUnderLeftCorner1.X and TopUnderLeftCorner.X > TopUnderRightCorner1.X and TopUnderLeftCorner.Y < TopUnderRightCorner1.Y and TopUnderLeftCorner.Y > BottomUpperRightCorner1.Y and TopUnderLeftCorner.Z < TopUpperRightCorner1.Z and TopUnderLeftCorner.Z > TopUnderRightCorner1.Z
		and TopUnderLeftCorner.X < BottomUpperLeftCorner1.X and TopUnderLeftCorner.X > BottomUpperRightCorner1.X and TopUnderLeftCorner.Y < TopUpperRightCorner1.Y and TopUnderLeftCorner.Y > BottomUnderRightCorner1.Y and TopUnderLeftCorner.Z < BottomUnderRightCorner1.Z and TopUnderLeftCorner.Z > BottomUpperRightCorner1.Z
		and TopUnderLeftCorner.X < TopUpperLeftCorner1.X and TopUnderLeftCorner.X > TopUpperRightCorner1.X and TopUnderLeftCorner.Y < TopUpperLeftCorner1.Y and TopUnderLeftCorner.Y > BottomUnderLeftCorner1.Y and TopUnderLeftCorner.Z < TopUpperLeftCorner1.Z and TopUnderLeftCorner.Z > TopUnderLeftCorner1.Z
		and TopUnderLeftCorner.X < BottomUnderLeftCorner1.X and TopUnderLeftCorner.X > BottomUnderRightCorner1.X and TopUnderLeftCorner.Y < TopUnderLeftCorner1.Y and TopUnderLeftCorner.Y > BottomUpperLeftCorner1.Y and TopUnderLeftCorner.Z < BottomUnderLeftCorner1.Z and TopUnderLeftCorner.Z > BottomUpperLeftCorner1.Z
		and TopUpperLeftCorner.X < TopUnderLeftCorner1.X and TopUpperLeftCorner.X > TopUnderRightCorner1.X and TopUpperLeftCorner.Y < TopUnderRightCorner1.Y and TopUpperLeftCorner.Y > BottomUpperRightCorner1.Y and TopUpperLeftCorner.Z < TopUpperRightCorner1.Z and TopUpperLeftCorner.Z > TopUnderRightCorner1.Z
		and TopUpperLeftCorner.X < BottomUpperLeftCorner1.X and TopUpperLeftCorner.X > BottomUpperRightCorner1.X and TopUpperLeftCorner.Y < TopUpperRightCorner1.Y and TopUpperLeftCorner.Y > BottomUnderRightCorner1.Y and TopUpperLeftCorner.Z < BottomUnderRightCorner1.Z and TopUpperLeftCorner.Z > BottomUpperRightCorner1.Z
		and TopUpperLeftCorner.X < TopUpperLeftCorner1.X and TopUpperLeftCorner.X > TopUpperRightCorner1.X and TopUpperLeftCorner.Y < TopUpperLeftCorner1.Y and TopUpperLeftCorner.Y > BottomUnderLeftCorner1.Y and TopUpperLeftCorner.Z < TopUpperLeftCorner1.Z and TopUpperLeftCorner.Z > TopUnderLeftCorner1.Z
		and TopUpperLeftCorner.X < BottomUnderLeftCorner1.X and TopUpperLeftCorner.X > BottomUnderRightCorner1.X and TopUpperLeftCorner.Y < TopUnderLeftCorner1.Y and TopUpperLeftCorner.Y > BottomUpperLeftCorner1.Y and TopUpperLeftCorner.Z < BottomUnderLeftCorner1.Z and TopUpperLeftCorner.Z > BottomUpperLeftCorner1.Z
		and BottomUpperLeftCorner.X < TopUnderLeftCorner1.X and BottomUpperLeftCorner.X > TopUnderRightCorner1.X and BottomUpperLeftCorner.Y < TopUnderRightCorner1.Y and BottomUpperLeftCorner.Y > BottomUpperRightCorner1.Y and BottomUpperLeftCorner.Z < TopUpperRightCorner1.Z and BottomUpperLeftCorner.Z > TopUnderRightCorner1.Z
		and BottomUpperLeftCorner.X < BottomUpperLeftCorner1.X and BottomUpperLeftCorner.X > BottomUpperRightCorner1.X and BottomUpperLeftCorner.Y < TopUpperRightCorner1.Y and BottomUpperLeftCorner.Y > BottomUnderRightCorner1.Y and BottomUpperLeftCorner.Z < BottomUnderRightCorner1.Z and BottomUpperLeftCorner.Z > BottomUpperRightCorner1.Z
		and BottomUpperLeftCorner.X < TopUpperLeftCorner1.X and BottomUpperLeftCorner.X > TopUpperRightCorner1.X and BottomUpperLeftCorner.Y < TopUpperLeftCorner1.Y and BottomUpperLeftCorner.Y > BottomUnderLeftCorner1.Y and BottomUpperLeftCorner.Z < TopUpperLeftCorner1.Z and BottomUpperLeftCorner.Z > TopUnderLeftCorner1.Z
		and BottomUpperLeftCorner.X < BottomUnderLeftCorner1.X and BottomUpperLeftCorner.X > BottomUnderRightCorner1.X and BottomUpperLeftCorner.Y < TopUnderLeftCorner1.Y and BottomUpperLeftCorner.Y > BottomUpperLeftCorner1.Y and BottomUpperLeftCorner.Z < BottomUnderLeftCorner1.Z and BottomUpperLeftCorner.Z > BottomUpperLeftCorner1.Z
		and BottomUnderLeftCorner.X < TopUnderLeftCorner1.X and BottomUnderLeftCorner.X > TopUnderRightCorner1.X and BottomUnderLeftCorner.Y < TopUnderRightCorner1.Y and BottomUnderLeftCorner.Y > BottomUpperRightCorner1.Y and BottomUnderLeftCorner.Z < TopUpperRightCorner1.Z and BottomUnderLeftCorner.Z > TopUnderRightCorner1.Z
		and BottomUnderLeftCorner.X < BottomUpperLeftCorner1.X and BottomUnderLeftCorner.X > BottomUpperRightCorner1.X and BottomUnderLeftCorner.Y < TopUpperRightCorner1.Y and BottomUnderLeftCorner.Y > BottomUnderRightCorner1.Y and BottomUnderLeftCorner.Z < BottomUnderRightCorner1.Z and BottomUnderLeftCorner.Z > BottomUpperRightCorner1.Z
		and BottomUnderLeftCorner.X < TopUpperLeftCorner1.X and BottomUnderLeftCorner.X > TopUpperRightCorner1.X and BottomUnderLeftCorner.Y < TopUpperLeftCorner1.Y and BottomUnderLeftCorner.Y > BottomUnderLeftCorner1.Y and BottomUnderLeftCorner.Z < TopUpperLeftCorner1.Z and BottomUnderLeftCorner.Z > TopUnderLeftCorner1.Z
		and BottomUnderLeftCorner.X < BottomUnderLeftCorner1.X and BottomUnderLeftCorner.X > BottomUnderRightCorner1.X and BottomUnderLeftCorner.Y < TopUnderLeftCorner1.Y and BottomUnderLeftCorner.Y > BottomUpperLeftCorner1.Y and BottomUnderLeftCorner.Z < BottomUnderLeftCorner1.Z and BottomUnderLeftCorner.Z > BottomUpperLeftCorner1.Z
		then

which you use five times in the code (pretty sure they are identical - but if not, you could easily just create two tables).

Since you define your variables earlier on in the code, this won’t impact the references or the values of your variables. I’m fairly certain you won’t get any sort of bug by doing it this way (and it should save you ~120 lines of code) since it’s using the same comparison syntax that you are using. If you’re seeing a bug that I’m not, please let me know as I’m genuinely curious if I’ve overlooked something.

1 Like

@https_KingPie, sorry if this has taken so long for reply to you, i never had a got occasion to test your code because the school. Anyway it work fine, thanks very much!

1 Like