Finding Left, Right, Rear, and Front sides of model

I have a script that is designed to find all 4 sides of a model and then create parts on each side; refer to the ColorTbl to see what color is what side. The issue is that it works on some models, while others it puts the Front and Back on Top and bottom? I don’t really get whats happening.

Code:

local VehicleSystem = {}

function VehicleSystem:CreateDetectors(Model)
	local Collection = game:GetService('CollectionService')
	local center, size = Model:GetBoundingBox()

	local thinThickness = 1  

	local frontBackSize = Vector3.new(size.X, size.Y, thinThickness)
	local leftRightSize = Vector3.new(thinThickness, size.Y, size.Z)

	local halfDepth = size.Z / 2
	local halfWidth = size.X / 2

	local Parts = {}
	local ColorTbl = {
		["Right"] = Color3.fromRGB(255,255,255), 
		["Left"] = Color3.fromRGB(0,0,0), 
		["Front"] = Color3.fromRGB(255,0,0), 
		["Back"] = Color3.fromRGB(0,0,255)
	}
	local Debug = true

	local function CreateRegion(Place, DirectionVector, Size)
		local Part = Instance.new("Part", workspace.PartsDebug)
		Part.Name = Place
		Part.Size = Size
		Part.Anchored = true
		Part.CFrame = DirectionVector
		Part.Transparency = 0.5
		Part.CanCollide = false
		Part.CanQuery = false
		Parts[Place] = Part
		Part.Color = ColorTbl[Place]
	end

	CreateRegion("Front", center * CFrame.new(0, 0, -halfDepth - thinThickness / 2), frontBackSize)
	CreateRegion("Back", center * CFrame.new(0, 0, halfDepth + thinThickness / 2), frontBackSize)
	CreateRegion("Left", center * CFrame.new(-halfWidth - thinThickness / 2, 0, 0), leftRightSize)
	CreateRegion("Right", center * CFrame.new(halfWidth + thinThickness / 2, 0, 0), leftRightSize)

end

for i,v in pairs(workspace.PartsDebug:GetChildren()) do
	v:Destroy()
end

for i,v in pairs(workspace.Folder:GetChildren()) do 
	VehicleSystem:CreateDetectors(v)
end

Picture:

Goes in order of which model is facing what way: Back, Left, Right, Forward

1 Like

Are they separate car models? (or did you duplicate and rotate them) If they are separate, I suspect it’s not the code but the orientation of the Primary Part in each car. Make sure all of the PrimaryPart’s Z-axis and front face is facing the front of the car.