# Check if a part is completely inside another

1. What do you want to achieve? I want to make a placement plot system. In this script it should be checked whether the player builds the parts out of the plot area.

2. What is the issue? I’ve already gotten it to check if the size and position is in the plot (BasePart) but can’t get it to check if the part is inside after rotating it.

3. What solutions have you tried so far? I’ve searched for posts here and tried chatgpt but unfortunately without a solution

``````local BasePart = game.Workspace.Plot1 -- The part that is acting as our boundaries
local ControlPart = script.Parent.Main -- The part we'll be checking

local Vectors = {
Vector3.new(0,0,1),
Vector3.new(0,0,-1),
Vector3.new(1,0,0),
Vector3.new(-1,0,0),
Vector3.new(0,1,0),
Vector3.new(0,-1,0)
}

local function CheckIfPartIsInsideAnotherPart ()
for _, Vector in pairs(Vectors) do
local BaseEnd = BasePart.Position + (BasePart.Size * Vector)/2 --Finds the edge of the base part
local ControlEnd = ControlPart.Position + (ControlPart.Size * Vector)/2 --Finds the edge of the control part

local BaseDirection = (BasePart.Position - BaseEnd).Unit
local ControlDirection = (BaseEnd - ControlEnd).Unit * BaseDirection

--What we'll end up doing is checking the unit direction of the base part and the control part, compared it to the BaseDirection
--What ends up happening is if the direction is negative, it's in bounds. if it's positive, it's out of bounds
if ControlDirection.X > 0 or ControlDirection.Y > 0 or ControlDirection.Z > 0 then
return false
end
end
return true
end
``````

Problem:

1 Like

I’m starting to despair I’ve even joined many discord servers and have followed up no one had an answer to it that was probably too complicated or not possible

1 Like

Can someone help me pls because i have no idea

1 Like

It seems that the current code is checking if the ControlPart is inside the BasePart by iterating through each of the six directions defined in the Vectors table. However, this approach doesn’t take into account the rotation of the ControlPart.

One possible solution is to convert the vertices of the ControlPart’s bounding box into world space, and then check if each vertex is inside the BasePart’s bounding box. Here’s an example implementation:

``````local function CheckIfPartIsInsideAnotherPart()
-- Get the world space vertices of the ControlPart's bounding box
local vertices = {}
for _, part in pairs(ControlPart:GetChildren()) do
if part:IsA("BasePart") then
for _, vertex in pairs(part:GetVertices()) do
table.insert(vertices, part.CFrame:PointToWorldSpace(vertex))
end
end
end

-- Check if each vertex is inside the BasePart's bounding box
for _, vertex in pairs(vertices) do
if not BasePart:BoundingBoxContainsPoint(vertex) then
return false
end
end

return true
end
``````

This implementation first iterates through all the BaseParts inside the ControlPart and gets their vertices in world space. Then it checks if each vertex is inside the BasePart’s bounding box using the BoundingBoxContainsPoint method. If any vertex is outside the bounding box, the function returns false, indicating that the ControlPart is not fully inside the BasePart.

1 Like