function moveToRandomPositionWithinBounds(targetPart, movingPart)
-- Get the size and CFrame of the target part
local targetSize = targetPart.Size
local targetCFrame = targetPart.CFrame
-- Calculate the local min and max bounds of the target part
local minX = -targetSize.X / 2
local maxX = targetSize.X / 2
local minY = -targetSize.Y / 2
local maxY = targetSize.Y / 2
local minZ = -targetSize.Z / 2
local maxZ = targetSize.Z / 2
-- Generate a random local position within the bounds
local randomX = math.random() * (maxX - minX) + minX
local randomY = math.random() * (maxY - minY) + minY
local randomZ = math.random() * (maxZ - minZ) + minZ
local randomLocalPosition = Vector3.new(randomX, randomY, randomZ)
-- Convert the local position to a world position
local randomWorldPosition = targetCFrame:PointToWorldSpace(randomLocalPosition)
-- Move the moving part to the random world position
movingPart.CFrame = CFrame.new(randomWorldPosition)
end
This’ll move movingPart into a random position within targetPart. It doesn’t account for movingPart’s size, so it’s possible that it gets moved onto the edge (which means it’d extend outside the part).