Not determining if part is inside another part properly

function isInsideZone(zone, position)
    local v3 = zone.CFrame:PointToObjectSpace(position)
    return (math.abs(v3.X) <= zone.Size.X / 2)
        and (math.abs(v3.Y) <= zone.Size.Y / 2)
        and (math.abs(v3.Z) <= zone.Size.Z / 2)
end
end

Zone represents a part and position represents position of another part.
So if the position is inside that zone then it returns true.

But if the Part and Zone are the same sizes, then it will only work if we remove the " / 2 " part, but if part is 2 times smaller than the Zone then it will only work if we make it " / 4 ", and for majority of other situations when part is smaller than the zone then " / 2 " works, how do i automatically find what to divide it with for it to always work with all Zone and Part sizes (maybe even multiply if part’s size is bigger than zone’s ???)

Your code snippet only checks if the given position is inside the zone part, and you’re only giving it the center of the part you’re checking.

If you still want to use that code snippet, you’ll need to repeat it for every single possible point in your part – which doesn’t sound right.

The best way to know if a part is inside another part is to just use workspace:GetPartsInPart(zone) and check if the part you’re looking for is in that array.

You should be using GetPartBoundsInBox to check all parts in a box.

Code:

local zoneCFrame = CFrame.new(0, 0, 0)
local zoneSize = Vector3.new(5, 5, 5)
local parts = workspace:GetPartsBoundInBox(zoneCFrame, zoneSize)

This is my function I use for solving that

function findNearestDungeon(pos, size)
    -- loop through the existing dungeons
    local x1 =(pos.x - size.X / 2) -- left edge
    local x2 = (pos.x + size.X / 2)-- right edge
    local z1 = (pos.z - size.Z / 2) -- bottom edge
    local z2 = (pos.z + size.Z / 2) -- top edge
    local rad=z1
    if x1<z1 then
        rad=x1     
    end
    for i = 1, #DungeonPosition do   
        -- get the coordinates of the edges of an existing dungeon
        local x3 = DungeonPosition[i].x - DungeonSize[i].X / 2 -- left edge
        local x4 = DungeonPosition[i].x + DungeonSize[i].X / 2 -- right edge
        local z3 = DungeonPosition[i].z - DungeonSize[i].Z / 2 -- bottom edge
        local z4 = DungeonPosition[i].z + DungeonSize[i].Z / 2 -- top edge
    if (math.max(x1, x3) < math.min(x2, x4) and math.max(z1, z3) < math.min(z2, z4)) or (pos-DungeonPosition[i]).Magnitude<rad  then
            -- overlap, return the direction of the overlap as a string
            if x1 < x3 then -- new dungeon is on the left of existing dungeon
                return "width"
            elseif x2 > x4 then -- new dungeon is on the right of existing dungeon
                return "width"
            elseif z1 < z3 then -- new dungeon is below existing dungeon
                return "length"
            elseif z2 > z4 then -- new dungeon is above existing dungeon
                return "length"
            else -- new dungeon is completely inside existing dungeon
                return "inside"
            end
        end
    end
    return nil -- no overlap
end